WinUI3: Non-Local Converters

I recently wrote a Windows App/WinUI3 application to start learning what will hopefully be Microsoft’s Windows desktop UI framework for some years. I come from having some familiarity with Windows Presentation Foundation/WPF, and none at all with Universal Windows Platform/UWP.

That’s a pretty big gap. Worse yet, while the basic elements of XAML haven’t changed much, a number of important details have. I found myself tripping over things where an approach that worked in WPF just doesn’t apply to WinUI3. One of those had to do with defining resources in another assembly.

Down with pack://!

Truth to tell, I was never all that clear on how to do that in WPF. The pack:// syntax always made my eyes glaze over. Fortunately, pack:// has been supplanted, at least in some places, by the somewhat simpler ms-appx:// approach. Its basic use is pretty straightforward:

<Application
    x:Class="J4JSoftware.GPSLocator.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:J4JSoftware.GPSLocator">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />

                <!-- Other merged dictionaries here -->
                <ResourceDictionary Source="ms-appx:///ui/common/DefaultStyles.xaml"/>
                <ResourceDictionary Source="ms-appx:///J4JSoftware.GPSCommon/converters/Converters.xaml"/>

            </ResourceDictionary.MergedDictionaries>
            <!-- Other app resources here -->

        </ResourceDictionary>
    </Application.Resources>
</Application>

Line 12 shows a simple reference to a DefaultStyles.xaml file in the current project. In this context ms-appx:// maps to a path relative to the project directory root. You can even see that in how the path starts, /ui/common…

Not So Fast!

But notice line 13. That’s more of a head-scratcher. It’s referring to a path rooted in the project directory of another project J4JSoftware.GPSCommon. So a better “definition” of ms-appx:// might be “a path relative to the current project root, unless you first specify a different project, in which case it’s a path relative to that project’s root”.

What made this difficult to figure out is that the Microsoft documentation — as per usual — tells you all sorts of things about ms-appx://…but doesn’t offer a framework for understanding its syntax. Or if it does it’s buried so far down that I couldn’t find it. Perennial note to Microsoft: reference materials are great, but only when viewed in the context of what the reader knows. You (generally) need to provide more introductory/framing context, not just the fascinating arcane whiz-bang details that will amaze and impress your readers who already pretty much know what they’re doing.

As I recall intelligent completion didn’t help me figure this out, either. Type that third ‘/’ and you’ll immediately be given path choices related to your current project. And not a hint that you can also reference stuff in other projects. I only found “the answer” after about 45 minutes of trial and error, when I finally — accidentally — tumbled onto the “referencing other projects” capability.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Archives
Categories