This document discusses creating custom markup extensions in XAML to define reusable descriptive elements. It provides examples of using bindings and resources as markup extensions. It also describes how to implement a custom markup extension by deriving from MarkupExtension and flagging the return type. Platform compatibility and differences when using markup extensions in UWP are also covered. Samples of using custom markup extensions for localization, bindings with converters, and event bindings are given.
2. Andreas Kuntner
• Senior Frontend Dev @
• C# .NET Development
• Client, UI, Mobile development
• App and library publications
• Developer’s blog
http://www.mobilemotion.eu
3. Let’s start with some XAML code
<Rectangle>
<Rectangle.Fill>
<SolidColorBrush Color="#FF0000"/>
</Rectangle.Fill>
</Rectangle>
10. Xamarin.Forms Markup Extensions
• Implement the IMarkupExtension interface
• To flag return type, use
IMarkupExtension<T>
and
public T ProvideValue(IServiceProvider s)
11. Custom Markup Extensions vs. UWP
MarkupExtension class is not available, but:
• as a workaround for fetching resources etc., use
CustomXamlResourceLoader
• Override its GetResource method and retrieve:
• one parameter
• target object
• target property
• target property type
• Don’t forget to register your loader during App startup!