Categories
Windows XAML

UWP and XAML CustomResource Markup

UWP doesn’t support writing custom markup extensions however there is one built in markup extension which is extensible. The CustomResource extension allows you to write XAML such as:-

<TextBlock Text="{CustomResource PortableStringResource1}"/>

In this case PortableStringResource1 is a unique key to refer to a resource in a source of your choosing. There is no out of the box implementation so you need to create your own class which derives from Windows.UI.Xaml.Resources.CustomXamlResourceLoader and provide an implementation of GetResource. Then in code which runs during your app start-up you create a new instance of your custom class and assign it to CustomXamlResourceLoader.Current. Then all requests are passed to your code to be resolved.

One place you may want to use this is where you are getting string resources from a different source than the modern resource system. In a pure UWP approach you can set an x:Uid for a XAML control then add string resources of the form MyId.SomeProperty to set the specified property from the resource. Another place you might have resources defined might be in a Portable Class Library. Because these work across multiple projects you might have string resources specified in a .resx file which you use with a number of projects (think Xamarin etc) and you may wish to also use these from a UWP client. Because of the way these resources are built into the UWP project you can access them via a Windows.ApplicationModel.Resources.ResourceLoader provided you specify the name of the resource set. This is the full namespace and classname of the generated class in the PCL. For example if your PCL has a base namespace of PortableClassLibrary and you have a resx file called StringResources.resx this is “PortableClassLibrary.StringResources”.

For the platforms where it is supported I’ve added a WindowsXamlResourceLoader class to InTheHand.UI. This can be used to refer to either resources defined in the UWP application resw or a named resource set in a referenced library. The source can be found here:-

https://github.com/inthehand/Charming/blob/master/Source/InTheHand/UI/Xaml/Resources/WindowsXamlResourceLoader.cs

An example of setting up the resource loader is called in the App.xaml.cs App() constructor:-

Windows.UI.Xaml.Resources.CustomXamlResourceLoader.Current = new InTheHand.UI.Xaml.Resources.WindowsXamlResourceLoader(typeof(PortableClassLibrary.StringResources).FullName);

The sample project for this blog contains the UwpClientApp and PortableClassLibrary projects to demonstrate the complete solution.

This is just one example of harnessing the CustomResource markup extension for your own use. You could write a resource loader to return values from any other source, you supply the glue to hook it together. The only limitation is you can’t really change the resource loader while dipping in and out of parts of your app so you need just one implementation. You could work around this by prefixing the key with different values linked to different sources…

By Peter Foot

Microsoft Windows Development MVP