How to use resources (fonts, images, styles) from external library - c#

I'm having problems with loading resources (images, font families, styles) from an external resource dll. I would like to have all resources (images, fonts, styles...) in one resource dll and set them to be accessible over all projects (libraries) in this solution. Those projects in solution are different libraries that are referenced and called by main application.
So far i tried several different propositions how to do it but none of them work.
I'm using Visual studio 2019 and compiling for .net 4.6.2 - if that means something...
First I created an resource library called myapp.resources. Inside this project I have a folder named Fonts and inside that folder is the Lato-Thin font.
Also at the root of the project I created a resource dictionary called Fonts.xaml
-Project
-Fonts
-Lato-Thin.ttf
-Fonts.xaml
the structure of the Fonts.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:myapp.resources">
<FontFamily x:Key="LatoThin">Fonts/#Lato Thin</FontFamily>
</ResourceDictionary>
In the main app App.xaml I added the loading of that ResourceDictionary:
<ResourceDictionary x:Key="Dict">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/myapp.resources;component/Fonts.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
The last step is adding this fontfamily to label control:
<Label Grid.Row="0" Grid.Column="0" Content="some random text for test" FontFamily="{StaticResource ResourceKey=LatoThin}"/>
But I always get the error that the LatoThin font resource can't be located.
The same error I get with any other resource type like images, styles.
Of course I added the references to projects and every file is where it should be.
The only thing that is working is, for example adding image to buttons in this way.
<syncfusion:ButtonAdv x:Name="btnSelectFile"
Grid.Row="1" Grid.Column="2"
VerticalAlignment="Center" SizeMode="Small"
Height="26" Width="26" Label="Button" Margin="3,3,3,3"
SmallIcon="pack://application:,,,/myapp.resources;component/Images/add.png"/>
I tried to find some complete tutorials to follow but I always have the same error.
So, my question would be: How to solve this?
Thanks for any advice.

Make sure your font file (Lato-Thin.ttf) has its build action set to Resource.
In your Fonts.xaml make sure to use this format:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:myapp.resources">
<FontFamily x:Key="LatoThin">/myapp.resources;component/Fonts/#Lato Thin</FontFamily>
</ResourceDictionary>
Tip: You can remove pack://application:,,, from your URIs to make them shorter.

Related

WinUI XAML: Using a ResourceDictionary from another project

In my WinUI 3 application, I am trying to use a ResourceDictionary that is located in another project.
Lets say the referenced project is ResourceTestLib and this library project has a folder "Styles" which has a file "_Thickness.xaml".
In the app.xaml file of the main application, I tried these two approaches below the "Other merged dictionaries here" comment, but none of them seem to work, i.e. that app crashes on startup with that message "Cannot locate resource ...." message.
This is my app.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<!-- Other merged dictionaries here -->
<ResourceDictionary Source="/ResourceTestLib;component/Styles/_Thickness.xaml"/>
<ResourceDictionary Source="pack://application:,,,/ResourceTestLib;component/Styles/_Thickness.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
When I hover over the respective line, Visual Studio shows a tooltip with "Path X not found" for the first referenced ResourceDictionary and a "Invalid characters in path" (probably because of "application:,,,") message for the second.
I suppose that that WinUI XAML might be different than WPF XAML in that respect? Or even that this is not supported yet?
Pack URIs are only used in WPF.
UWP and Win UI use the ms-appx URI scheme to refer to a file that resides in another assembly so try this:
<ResourceDictionary Source="ms-appx:///ResourceTestLib/Styles/_Thickness.xaml" />
More on UWP style URI schemes:
https://learn.microsoft.com/en-us/windows/uwp/app-resources/uri-schemes

How to include MaterialDesignXamlToolkit to WPF class library?

I'm trying to use MaterialDesignXamlToolkit in my WPF class library (.NET framework). I'm following their official quick start tutorial, but since i do not have App.xaml, i had to make some adjustments. Apperently some step was wrong, but i do not know which one.
1) I installed MaterialDesignXamlToolkit using Nuget.
2) I created ResourceDictionary with the following code: (i specified the key because there is an error if i don't)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary x:Key="123">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</ResourceDictionary>
If i remove <ResourceDictionary x:Key="123"> element, then i get an error:
System.Windows.Markup.XamlParseException: Set property 'System.Windows.ResourceDictionary.Source' threw an exception.
FileNotFoundException: Could not load file or assembly 'MaterialDesignThemes.Wpf, Culture=neutral' or one of its dependencies.
3) My 'main screen' is Page, so i added the resource to it:
<Page.Resources>
<ResourceDictionary Source="/MyAsembly;component/ResourceDictionary/MaterialDesign.xaml" />
</Page.Resources>
4) The obvious problem occurs here (this is the second step of the official tutorial): i add the following code to my Page:
<Page ...
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
TextElement.Foreground="{DynamicResource MaterialDesignBody}"
TextElement.FontWeight="Regular"
TextElement.FontSize="13"
TextOptions.TextFormattingMode="Ideal"
TextOptions.TextRenderingMode="Auto"
Background="{DynamicResource MaterialDesignPaper}"
FontFamily="{DynamicResource MaterialDesignFont}">
But i get a warning that: The resource {MaterialDesignBody, MaterialDesignPaper, MaterialDesignFont} could not be resolved.
Some of the solutions i tried pointed out that the ResourceDictionary's build action should be page, and it is.
Any help would be greatly appreciated!
The accepted solution worked for me. To avoid the dummy code though, I was also able to get MDXT working by adding the following to the code-behind of the resource dictionary:
Assembly.LoadFrom(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "MaterialDesignThemes.Wpf.dll"));
Assembly.LoadFrom(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "MaterialDesignColors.dll"));
Now that i've solved the problem, i realize one important information is missing from my question: i was following MVVM pattern (so all my code behind files were empty).
The problem was with the way Revit (the application that i was building a plugin for) loads libraries that a plugin is using. I still do not understand the internal logic of it, but the following two lines added to the code behind of the first page what is being loaded solved the problem for me:
ColorZoneAssist.SetMode(new GroupBox(), ColorZoneMode.Accent);
Hue hue = new Hue("name", System.Windows.Media.Color.FromArgb(1, 2, 3, 4), System.Windows.Media.Color.FromArgb(1, 5, 6, 7));
I cannot stress enought that those two lines of code are a complite bullshit (since i do not want to place any logic to code behind), but the libraries won't otherwise be loaded. This code somehow 'forces' Revit to load Material design libraries (1st line of code uses MaterialDesignTheme.Wpf, and the 2nd MaterialDesignColors), since (i assume) it can already tell at compile time that those libraries are needed.
Remove the <ResourceDictionary x:Key="123"> element from your ResourceDictionary to begin with:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
You should then be able to set the properties using property element syntax after you have set the Resources property:
<Page ...
d:DesignHeight="450" d:DesignWidth="800">
<Page.Resources>
<ResourceDictionary Source="/MyAsembly;component/ResourceDictionary/MaterialDesign.xaml" />
</Page.Resources>
<Page.Background>
<DynamicResource ResourceKey="MaterialDesignPaper" />
</Page.Background>
</Page>
Without adding those lines.
Double check if the MaterialDesign dll file get copied to the output path of the application.
I have seen such issue before, just adding nonsense code and Visual Studio realize your application that depends on your lib also depends on MaterialDesign lib and then copies the dll again as one would expect in the first place.
Instead of adding those lines you could then
Reference MaterialDesign directly in your application as well
Use a build event to make sure the DLL is copied to the build path.
This comment solves the problem for me,
but make sure you don't have another errors and if you have just find them and fix theme then try to run the project and it will work.
using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
public MainWindow()
{
InitializeMaterialDesign();
InitializeComponent();
}
private void InitializeMaterialDesign()
{
// Create dummy objects to force the MaterialDesign assemblies to be loaded
// from this assembly, which causes the MaterialDesign assemblies to be searched
// relative to this assembly's path. Otherwise, the MaterialDesign assemblies
// are searched relative to Eclipse's path, so they're not found.
var card = new Card();
var hue = new Hue("Dummy", Colors.Black, Colors.White);
}

Load xaml ResourceDictionary from dll

I have made several ResourceDictionaries for our applications team to use with their future applications. I have deployed the contents of the class library project containing these dictionaries to a .dll file and would like to be able to use the dictionaries by adding a reference to the .dll file in a new WPF solution where I hope to make a new application.
The Class Library in my example is called "NWF_Class_Library.dll" and is saved in the same folder in windows explorer as the MainWindow.xaml file. Is it possible to retrieve the resource dictionaries from within it?
I have read articles about the best way for an organisation to arrange their xaml resources, so it seems it must be possible, but all I find is ways to use the "//pack:application:..." syntax to reference xaml within the same solution as the wpf application. Here is a snippet of code, with the Source blank because nothing I have written has worked!
We had hoped that we could add the standard configurations as well as our more normal useful methods etc to a file that can be deployed with applications.
<Window x:Class="dll_ref_included.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ResourceDictionary Source=""/>
</Window.Resources>
<Grid>
<Button Style="{StaticResource myButton}">This</Button>
</Grid>
</Window>
Try this:
<ResourceDictionary Source="pack://application:,,,/NWF_Class_Library;component/Dictionary1.xaml"/>
...where "NWF_Class_Library" is the name of the referenced assembly and "Dictionary1.xaml" is the name of the ResourceDictionary that is defined in this project.
You can refer to the documentation for more information about pack URIs and how to use them.

multilingual wpf application

I have a WPF application (in English) and I would like to let users to select different languages. I have read some possibilities to change languages in runtime applications, but I only want to choose a language during installation time and never change it.
Do you think the fastest and easiest way to do it is developing different versions of the program (changing only text language) and let the user to select one of them during the installation?? Probably to repeat code only changing textbox or labels is not very elegant, but notice that I have the application finished in English and I donĀ“t need to change language at runtime.
You can follow these steps:
Creating the resource files
Add this file StringResources.xaml to Resources directory. Here is an example:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="close">Close</system:String>
</ResourceDictionary>
You can create several files, one for each language.
Adding the resource (Call this when you start your application)
private void SetLanguageDictionary()
{
ResourceDictionary dict = new ResourceDictionary();
switch (Thread.CurrentThread.CurrentCulture.ToString())
{
case "en-US":
dict.Source = new Uri("..\\Resources\\StringResources.xaml", UriKind.Relative);
break;
case "fr-CA":
dict.Source = new Uri("..\\Resources\\StringResources.fr-CA.xaml", UriKind.Relative);
break;
default :
dict.Source = new Uri("..\\Resources\\StringResources.xaml",UriKind.Relative);
break;
}
this.Resources.MergedDictionaries.Add(dict);
}
Using the Resource, like this -
<Button
x:Name="btnLogin"
Click="btnLogin_Click"
Content="{DynamicResource close}"
Grid.Row="3"
Grid.Column="0"
Padding="10" />
Source: https://www.codeproject.com/Articles/123460/Simplest-Way-to-Implement-Multilingual-WPF-Applica
I think the solution proposed by Aghilas is good; but you can use StaticResource instead of using DynamicResource in step 3, DynamicResource is not required in your case as you are not going to chnage the language while application is running.
Also have a look at these articles having details about using Resx files for localization in WPF -
Localizing a WPF Application with ResX Files
WPF Localization
WPF Localization Guidance - Whitepaper
Just to improve #AghilasYakoub's correct answer, I think I need to point out that the following code should be added to the file App.xaml apart from what he had said:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/StringResources.xaml"/>
<ResourceDictionary Source="Resources/StringResources.fr-CA.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
If you want to use RESX files instead of resource dictionaries, you can do it easily with static references in XAML.
<Window x:Class="MyApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:res="clr-namespace:MyApp.Resources">
<Button Text="{x:Static res:MainWindow.MyTestKey}">
</Window>
In the Resource folder is the MainWindow.resx, MainWindow.de.resx, etc. and every file contains a key MyTestKey with a translation.

WPF Prism - Where to put Resources?

I have a prism application and various modules. I am wondering where is the best place to locate resources such as styles, brush, controltemplates, datatemplates?
Should I make one single resource dictionary and put everything there? Should each module have their own resources? Or each view? I would like to follow the Prism goal of keeping everything modular, but also I dont see the point in re-declaring the same resources in every module...
I develop application with Prism, and I use technique very close to described in Prism's manual. There is YourApplication.Infrastructure project, where you usually place all your shared interfaces etc. So:
I just add project YourApplication.Resources
Create there folder Themes
Create separate xaml file in Themes folder for each group of resources (like Generic.WPF.xaml for standard WPF controls' styles, Generic.Brushes.xaml for brushes etc.)
Create file Themes\Generic.xaml (exactly with this name, it will add huge benefits in the future) with content like
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Generic.Brushes.xaml"/>
<ResourceDictionary Source="Generic.WPF.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Now you can add those resources in any module (you have separate project for it, right?) by adding reference to YourApplication.Resources to that project and adding to your view's xaml:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/YourApplication.Resources;component/Themes/Generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- Put your not shared resource here -->
</ResourceDictionary>
</UserControl.Resources>
I don't know, maybe this way has some problems, but it works, and works well for me. If anybody can comment somehow this way (pros/cons) - I will be very happy to hear it!
Application-wide resources I usually put in a ResourceDictionary, which is added to either App.xaml or StartupWindow.xaml
Resources for a specific View are usually located with the View. For example, a UserControl that is being used for a CalendarView will contain any custom resources for the Calendar, such as calendar-specific brushes, styles, templates, etc.
I usually don't see a reason to make module-wide resources, but if I ever do I'd have a ResourceDictionary for the Module which can be loaded into the app's merged dictionaries at runtime, or included in individual Views in the Module.
I would like to share some new knowledges. I am using #chopikadze approach. And it is really cool approach. Thanks to you!
However, if you do not want write every time for each control these piece of code:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/YourApplication.Resources;component/Themes/Generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- Put your not shared resource here -->
</ResourceDictionary>
</UserControl.Resources>
Then you can just declare <ResourceDictionary/> in App.xaml of your Bootstrapper like that:
<Application.Resources>
<ResourceDictionary Source="pack://application:,,,/YourApplication.Resources;component/Themes/Generic.xaml"/>
</Application.Resources>

Categories