- darthmaule2
- Posts: 98
- Joined:
Is there a way to load xaml resources from a separate project?
Starting with the HelloWorld C# example, I see that XAML resources are stored in the .resx file and loaded like this:
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:
... 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?
Code: Select all
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
}
Code: Select all
<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>
...
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?
Re: Is there a way to load xaml resources from a separate project?
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
- Posts: 98
- Joined:
Re: Is there a way to load xaml resources from a separate project?
OK thanks. I have this working now. My custom xaml resource loader:
Which can be called like this:
Code: Select all
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)));
}
}
Code: Select all
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 }
});
}
Re: Is there a way to load xaml resources from a separate project?
Thanks for the great feedback!
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 20 guests