darthmaule2
Topic Author
Posts: 58
Joined: 23 Oct 2014, 19:54

Is there a way to load xaml resources from a separate project?

04 Jan 2019, 21:28

Starting with the HelloWorld C# example, I see that XAML resources are stored in the .resx file and loaded like this:
    public partial class App : Application
    {
#if NOESIS
        protected override XamlProvider GetXamlProvider()
        {
            EmbeddedXaml[] xamls =
            {
                new EmbeddedXaml { filename = "App.xaml", resource = "App" },
                new EmbeddedXaml { filename = "MainWindow.xaml", resource = "MainWindow" },
            };
            return new EmbeddedXamlProvider(xamls, Properties.Resources.ResourceManager);
        }
#endif
    }
I created a new C# class library project "Modules.FileManager" and defined a UserControl based view there called "FileManagerView.xaml". I would like load that view into the MainWindow.xaml of the HelloWorld project like this:
<Window>
    xmlns:fm="clr-namespace:Modules.FileManager;assembly=Modules.FileManager"
    ...
</Window>
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Modules.FileManager;component/Resources/FileManagerView.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    ...
    <fm:FileManagerView></fm:FileManagerView>
    ...
... but that is crashing because FileManagerView.xaml is undefined (because I added it to a separate resource file, one defined in the Modules.FileManager project, rather than the HelloWorld project.

I assume I need to have FileMangerView.xaml loaded into a resource file?
Do all the xaml resources have to be in the same resource file or can they be defined in separate resources files in each class library project and then merged together somehow?

Tags:
 
User avatar
jsantos
Site Admin
Posts: 2490
Joined: 20 Jan 2012, 17:18
Contact:

Re: Is there a way to load xaml resources from a separate project?

07 Jan 2019, 17:42

XAMLs and rest of resources like textures and fonts can be stored in any place you want. You just have to implement the corresponding provider that will return a Stream when asked for a resource. In our C++ samples we are storing all the resources inside the executable with a tool (bin2h) that converts binaries to header files. The EmbeddedXamlProvider is an implementation of provider that is able to create streams from those resources. We did the same for the C# SDK, the same EmbeddedXamlProvider is available in the samples, but nothing should stop you from implementing your own provider. I am not sure if in C# we should follow a different approach for our examples, like storing the resources in the assemblies, but I am not sure if that will be portable among all the C# platforms we have.
 
darthmaule2
Topic Author
Posts: 58
Joined: 23 Oct 2014, 19:54

Re: Is there a way to load xaml resources from a separate project?

07 Jan 2019, 21:00

OK thanks. I have this working now. My custom xaml resource loader:
    public class MultiProjectEmbeddedXamlProvider : XamlProvider
    {
        private Dictionary<string, ResourceManager> _xamlToResourceManager;
        public MultiProjectEmbeddedXamlProvider(Dictionary<string, ResourceManager> xamlToResourceManager)
        {
            _xamlToResourceManager = xamlToResourceManager;
        }

        public override Stream LoadXaml(string filename)
        {
            return new MemoryStream((byte[])_xamlToResourceManager[filename].GetObject(Path.GetFileNameWithoutExtension(filename)));
        }
    }
Which can be called like this:
 
protected override XamlProvider GetXamlProvider()
{
    return new MultiProjectEmbeddedXamlProvider(new Dictionary<string, ResourceManager>()
    {
        { "App.xaml", Properties.Resources.ResourceManager },
        { "AppResources.xaml", Properties.Resources.ResourceManager },                
        { "MainWindow.xaml", Properties.Resources.ResourceManager },
        { "FileManagerView.xaml", Modules.FileManager.Properties.Resources.ResourceManager }
    });
}
 
User avatar
jsantos
Site Admin
Posts: 2490
Joined: 20 Jan 2012, 17:18
Contact:

Re: Is there a way to load xaml resources from a separate project?

07 Jan 2019, 23:25

Thanks for the great feedback!

Who is online

Users browsing this forum: digimbyte and 2 guests