none
[UWP][VB][XAML]Learning to Create a Custom MarkupExtension RRS feed

  • Question

  • I am writing a UWP app, and am looking to learn to create custom MarkupExtension(s). I have never written a custom MarkupExtension (although I have, of course, used many standard existing ones). In this specific scenario, I am looking to make a MarkupExtension that will make it easier to enter Integer values (so that they are already Integers, and the codebehind does not need to worry about converting them). Everything I have found I have had trouble getting to work (probably because I'm new to custom MarkupExtension(s), I'm sure it can't be too hard once I get the basics), but I have not yet done that. Please keep in mind that I am using UWP (not WPF), although I am using the latest version, but I believe there are some differences. Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, August 14, 2019 3:53 AM

All replies

  • Hi,

    If you need to create custom MarkupExtension, please make sure that you are targeting at least build 16299 and as the same for min version. Then you could create a class inherited from MarkupExtension. Next you need to override the MarkupExtension.ProvideValue to provide the implementation and marked with the MarkupExtensionReturnTypeAttribute and specifies the return type. Like this:

    [Bindable]
        [MarkupExtensionReturnType(ReturnType =typeof(string))]
        public class PageDataContextBindingExtension : MarkupExtension
        {
            public string PropertyName { get; set; }
    
            protected override object ProvideValue()
            {
                return "This is test";
            }
        }

    finally you can then use it in XAML just like this:

    <Textblock Text="{eb:PageDataContextBinding PropertyName=111}"/>

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, August 14, 2019 2:04 PM
    Moderator