Search code examples
c#.netxamluwpmarkup-extensions

Custom MarkupExtension in UWP


I would like to create my own MarkupExtension (like Binding, TemplateBinding...)

How can I do it for Universal Apps like I did in WPF?


Solution

  • I has a sad, but no. UWP doesn't currently support custom markup extensions.

    In some cases you can work around this by using bindings and converters. For example, to use a resource string (not using x:Uid), I have a converter that doesn't actually need a value, only a parameter (the resource id).

    For example, I might bind as follows:

    <TextBlock
        Text="{x:Bind Language, Mode=OneWay, Converter={StaticResource Localize}, ConverterParameter=MyResourceId}"
    />
    

    (This binding doesn't need a path, but I bind to Language anyway, which allows switching languages on the fly.)

    More about the LocalizeConverter here.

    But anyway. If I'd been doing WPF, this would have been a markup extension. Binding itself is, of course, a markup extension, so as long as you can work within its constraints, it is a possible workaround.

    EDIT

    I just want to highlight the link Clemens gave in his comment to the OP:

    https://wpdev.uservoice.com/forums/110705-universal-windows-platform/suggestions/7232264-add-markup-extensions-to-and-improve-winrt-xaml

    UPDATE

    There is now limited support for custom markup extensions! (Hat tip to Michael Hawker's comment.)