Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

Dynamically loading XAML libraries in UWP and WinUI

License

Notifications You must be signed in to change notification settings

dongle-the-gadget/DynamicXAML

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DynamicXAML

DynamicXaml is a library that allows you to dynamically load XAML resources (such as XAML/XBF files embedded inside PRI files) at runtime in UWP and WinUI 3 applications.

With DynamicXaml you can dynamically load external XAML libraries at runtime, which is useful for scenarios like plugin systems, theming, or modular applications where you want to load UI components that aren't part of the application itself nor compiled with it.

Installation

NuGet packages are coming soon, for now you can clone the repository and build the solution to get the library.

Usage

On libraries side

Libraries have to be either native Windows Runtime Components or .NET Class Libraries.

Libraries should add the following property to their project (e.g. csproj, vcxproj, ...) file:

<DisableEmbeddedXbf>false</DisableEmbeddedXbf>

This property will ensure that all XAML resources are embedded into the library's PRI file, which is required for DynamicXaml to work.

Additional considerations for UWP libraries

Important

These considerations are only applicable to UWP libraries and only to manual usages of ms-appx:/// URIs, they do NOT apply to WinUI 3 libraries nor code generated by Xaml Compiler.

If the library is manually accessing its resources via ms-appx:/// URIs, these should be changed to include the library's PRI index name after the second slash like the following example:

<!-- Before -->
<ResourceDictionary Source="ms-appx:///My.Dynamic.Lib/Themes/Generic.xaml" />
<!-- After -->
<ResourceDictionary Source="ms-appx://My.Dynamic.Lib/My.Dynamic.Lib/Themes/Generic.xaml" />

This limitation exists because Windows.UI.Xaml (aka UWP XAML) uses a private MRM API to load these resources and DynamicXaml aims to avoid using or hooking any private APIs, this limitation however does NOT exist in WinUI 3 since it uses a public API (the WinRT MRT Core API) for loading these resources and DynamicXaml hooks that API and makes the required redirections for it to work.

On application side

To use DynamicXaml in your application, you need to add a reference to the library and then after initializing XAML, call DynamicLoader.LoadPri with a PRI file StorageFile, on WinUI 3 there's also an overload that accepts a path to the PRI file and a Try variant of the LoadPri function.

Example usage in C#:

using DynamicXaml;

...

// Load the PRI file
StorageFile priFile = await ApplicationData.Current.LocalFolder.GetFileAsync("My.Dynamic.Lib.pri");
DynamicLoader.LoadPri(priFile);

Then you can load the library dll and use the XAML resources defined in it, this is done via either NativeLibrary.Load (C#) or LoadLibrary (Native) in case of native libraries, or Assembly.Load in case of .NET libraries, in case of native libraries you can activate the XAML classes defined in it by calling the DllGetActivationFactory function exported by the dll.

There are sample projects in the repository that demonstrate how to use DynamicXaml for both Native and .NET libraries in both UWP and WinUI 3 applications.

About

Dynamically loading XAML libraries in UWP and WinUI

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 49.1%
  • C++ 46.3%
  • C 4.6%