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.
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);
}
Hello my team and I recently started developing an win10 uwp application. Application will have a lot of views and components so heavy use of styles is expected, so we need to organize our styles through file/folder structure we did this using following structure (unfortunately I cannot embed images yet see the link):
Anyways my Resource.xaml merges all other dictionaries as following:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/Colors.xaml" />
<ResourceDictionary Source="/Resources/Icons.xaml" />
<ResourceDictionary Source="/Resources/Fonts.xaml" />
<ResourceDictionary Source="/Resources/Converters.xaml" />
<ResourceDictionary Source="/Resources/Buttons.xaml" />
<ResourceDictionary Source="/Resources/RadioButton.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
And in my App.xaml I reference this dictionary:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Resources.xaml" />
</ResourceDictionary.MergedDictionaries>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
</ResourceDictionary>
</Application.Resources>
Now I managed to find the source of the problem in my RadioButton.xaml I reference a brush defined in Colors.xaml using StaticResource lookup:
<Setter Property="Foreground" Value="{StaticResource TopMenuTextBrush}" />
If I remove this line everything will start but with it I get following exception:
Exception {Windows.UI.Xaml.Markup.XamlParseException: The text associated with this error code could not be found.
Failed to assign to property
'Windows.UI.Xaml.ResourceDictionary.Source' because the type
'Windows.Foundation.String' cannot be assigned to the type
'Windows.Foundation.Uri'. [Line: 28 Position: 37]} System.Exception
{Windows.UI.Xaml.Markup.XamlParseException}
Interesting thing is when I start the app with this line commented and uncomment it visual studio will recognize the brush and apply it correctly, it only breaks on application start.
We used same approach before when developing WPF, so I'm thinking it might have to do with something regarding application deployment.
All help is greatly appreciated.
Exception = {Windows.UI.Xaml.Markup.XamlParseException: The text associated with this error code could not be found.
The problem is that you have used wrong ResourceDictionary source . I found the Resources.xaml and other xaml file stored in the same level directory in your screenshot. So you could not declare the parent directory of these xaml files within source. Please modify ResourceDictionary like the following
<ResourceDictionary Source="Colors.xaml"/>
For more you could refer to ResourceDictionary and XAML resource references.
Hello I'm using Modern UI for WPF ,I have a page that contains a list with Items as links to other pages(User controls) my problem is when I press a link I have the error
System.IO.IO exception cannot locate resource 'basicpage1.xaml'
I have searched a lot but with no hope.
here is my XAML file for the list page:
<UserControl x:Class="ModernUINavigationApp.Pages.ListPage1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Style="{StaticResource ContentRoot}">
<mui:ModernTab Layout="List" >
<mui:ModernTab.Links>
<!-- TODO: set #Source -->
<mui:Link DisplayName="Item 1" Source="/basicpage1.xaml"/>
<mui:Link DisplayName="Item 2" />
</mui:ModernTab.Links>
</mui:ModernTab>
</Grid>
</UserControl>
The Exception
Solution Explorer
basicpage1.xaml is located in your Pages directory so you should add /Pages to the source:
<mui:Link DisplayName="Item 1" Source="/Pages/basicpage1.xaml"/>
I just encountered the same kind of problem when setting a Frame Source to a page.
When checking file properties in solution explorer I noticed that VS has put the page file in a subdirectory of the application file (in my case VS named the subdirectory "My Project") so I rewrote
<Frame Source="/Page1.xaml"/>
to
<Frame Source="/My Project/Page1.xaml">
and it solved the problem.
Just check and compare the full path property of application.xaml versus your basicpage1.xaml"
I'm toying arround a bit with the new VS 2015, and I found two strange behaviors, I really can't explain. Probably you can help me, if this are simple bugs or Im doing something generally wrong:
Í have a simple Project, the Mainwindow has just a Datagrid:
<Grid>
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Header="Test" />
</DataGrid.Columns>
</DataGrid>
</Grid>
In the App.XAML I load a Dictionary in a Subfolder:
<Application.Resources>
<ResourceDictionary Source="Dictionaries\AppDictionary.xaml" />
</Application.Resources>
The Dictionary is looking like this:
<Style TargetType="Grid">
<Setter Property="Margin" Value="10" />
</Style>
Now, as soon as I load the Window, it seems to be frozen forever. I tested some Grid-Properties, but this happens only, when I set the Margin it seems.
I checked the Inheritance from DataGrid or DataGridTextColumn, but they don't seem to inherit from the Grid anyhow.
They second exception:
I need to create a Class in the Dictionaries-Subfolder, otherwise i keep getting the exception:
The type or
namespace name 'Dictionaries' does not exist in the namespace
'WpfApplication3' (are you missing an assembly
reference?)
I didn't work with WPF for a while, but I'm almost certain, I didn't have such problems before, but I might be wrong on this case.
Edit: When I break the Debugging I get the Message:
"Your app has entered a break state, but there is no code to show because all threads where executing external code".
Your information doesn't seem complete but It seems that you are not building the Dictionaries\AppDictionary.xaml file. You need to set its Build Action to Resource.
Also, your dictionary should look like this:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionaries\AppDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>