Search code examples
c#wpfmauiembedded-resourceresourcedictionary

Sharing Resources Between MAUI and WPF


I have been writing C#/WPF applications for years, but have started to experiment with some apps using MAUI.

Over time, I have gathered icons and other visual resources in to a class library which has the tags:

<TargetFramework>net7.0-windows</TargetFramework>
<UseWpf>true</UseWpf>

My icons are then loaded as XAML from ResourceDictionaries depending on the application theme etc. This has been a really nice way of having a single XAML file per icon set and loading them all from a single library.

With MAUI, I first attempted to remove the -windows and UseWpf tags so I was compiling pure shared net7.0. This, of course, means that my existing WPF applications fail to find the resources using Pack notation.

I realise I could have one resource assembly for WPF and another for MAUI, but I've already created fairly mature scripts to import new icons to all the icon sets, and if there's a way of sharing a single library, that's what I'd like to do.

Is there a way to achieve this?


Solution

  • Not easily.

    Maui might use xaml but it's a different flavour of xaml than wpf.

    A maui resource dictionary has different headings to a wpf one. You can't even share a resource dictionary file. https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/resource-dictionaries?view=net-maui-7.0

    <?xml version="1.0" encoding="UTF-8" ?>
    <?xaml-comp compile="true" ?>
    <ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    

    Whereas a wpf resource dictionary outer tag would be:

    <ResourceDictionary 
               
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    

    If you stored your resources as xml then you could build resource dictionaries on the fly for both.

    You'd have to be really careful you don't use something doesn't work in maui ( or wpf ).

    Even then, you'll have complications with things like pictures. Things that work in one will not in the other.

    The more I think about it the more I think this is a minefield.

    I am doubtful whether this would be practical. I think porting is likely to be much more viable. If your wpf resources are fairly mature then maybe they aren't going to change much.