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…

Advertisements

Windows Embedded Compact 2013 & Visual Studio 2013

The latest update to Windows Embedded Compact 2013 was a bit of a surprise as besides the usual cumulative bug fixes and tweaks it adds development support with Visual Studio 2015. Since the days of Windows Mobile 6 and even CE 7 it has been common to use an old version of Visual Studio to get things done but here the tools have jumped to the latest Visual Studio version.

Besides the usual Console and Windows Forms project types we’ve tested XAML In The Hand too and you can build managed code XAML based applications. You can debug over Ethernet, edit your XAML with intellisense, use NuGet packages etc all as you would with “big” Windows development.

xaml2015

One of the new features added in Compact 2013 was Behaviors and while you can use them already in XAML In The Hand projects from your XAML we’re investigating whether it might be useful to add code support for these too depending on demand.

The Compact 2013 installation is in two parts – Application Builder is the plugin for Visual Studio:-

http://www.microsoft.com/en-in/download/details.aspx?id=38819

The latest Compact 2013 Platform update is here:-

http://www.microsoft.com/en-us/download/details.aspx?id=42027

More details on XAML In The Hand can be found here:-

XAML In The Hand

 

Moving from WinForms to XAML Runtime on Windows Embedded Compact

I recently found an old set of slides I created for TechEd NZ a few years ago which collected together useful information for moving from Windows Mobile to Windows Phone. It struck me that some of this would also be useful for the Embedded XAML Runtime when considering moving code from a traditional WinForms UI to a XAML based UI using Xaml In The Hand. I’ve modified the table listing controls in both UI worlds and added it to the Knowledge Base here:-

http://inthehand.uservoice.com/knowledgebase/articles/209986-winforms-versus-xaml-runtime-controls

While most of this still applies to Windows Phone (and with a few further changes Windows Store apps) there are items here which are specific to Windows Embedded Compact – The ComboBox control which doesn’t exist on Phone (although there is a perfectly good alternative), and the MediaElement and WebBrowser controls which don’t exist in the XAML Runtime but for which we have created managed code controls which expose the same XAML and C# interfaces.

WriteableBitmapEx for Windows Embedded Compact 7

I have ported René Schulte‘s excellent WriteableBitmapEx project to run on Windows Embedded Compact 7. The original library supports drawing across various XAML user interfaces – Silverlight, Windows Phone, WPF and Windows 8 Apps. Because XAML In The Hand exposes an object model which matches Silverlight there was very little work required to port, it just needed a new Dll project for .NETCF 3.5 and a reference to the XAML In The Hand DLL. This allows a whole range of complex drawing operations to be performed where using Silverlight Paths and Shapes would be inefficient.

WriteableBitmap for Windows Embedded

Performance will vary more because the range of hardware platforms available for Windows Embedded Compact varies considerably, both in processing power and screen sizes. I’ve tested the code on FreeScale development boards at up to 1024×768 and on the new Motorola WT41N1 Wearable Computer which has a small 320×240 resistive touch display with encouraging results. Writing XAML user interfaces for embedded devices is incredibly easy once you’ve experienced the Windows Phone and desktop tools. With built in support for touch and dynamic layouts and all the animation and data-binding you would expect it allows you to write fluid user interfaces for specialist devices where a consumer phone or tablet would be impractical. More information on XAML In The Hand is available here