I'm having trouble resolving a component resource key in my WPF app for a DLL I was trying to use.
DLL source I'm trying to use:
https://www.codeproject.com/Articles/42227/Automatic-WPF-Toolkit-DataGrid-Filtering
Error message:
The resource "{ComponentResourceKey ResourceId=DataGridHeaderFilterControlStyle, TypeInTargetAssembly={x:Type ScoreBoardClientTest:DataGridHeaderFilterControl}}" could not be resolved.
I've compiled the DLL and added it as a reference to my project.
I've added the appropriate xmlns filter to my XAML see below:
xmlns:filter="clr-namespace:DataGridFilterLibrary;assembly=DataGridFilterLibrary"
When I try to use a columnheaderstyle in my data grid is when the error above is thrown
ColumnHeaderStyle="{StaticResource {ComponentResourceKey
TypeInTargetAssembly={x:Type filter:DataGridHeaderFilterControl},
ResourceId=DataGridHeaderFilterControlStyle}}"
any help determining what I'm doing wrong here would be great. Thought it was straight forward.
I run into the same problem, when I using the filter in a UserControl. In a Window it works as expected.
Fixed this problem with adding the Ressource in the UserControl:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/DataGridFilterLibrary;component/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Related
Is there a way to reference a local ResourceDictionary from within the same FrameworkElement's property?
I tried the following
<TextBlock Text="{StaticResource txt}">
<TextBlock.Resources>
<x:String x:Key="txt">asdf</x:String>
</TextBlock.Resources>
</TextBlock>
but get the error
The resource "txt" could not be resolved.
Moving the txt resource from the TextBlock to the Page resources would work but that seems messy and i was hoping that it's possible to reference a FrameworkElements local ResourceDictionary.
Using CustomResource instead of StaticResource at least allows to auto complete to txt, but it doesn't work because then invoking Initializecomponent throws an exception: "No custom resource loader set" and i'm not sure if implementing and setting a custom loader would solve that at all.
Is there a way to do this using the local resource dictionary?
UWP FrameworkElement resource lookup looks for parent control resources, not child control resources.
This document details how Resources works.
Edit
It is recommended to use Resource Dictionary file.
Right click on your project
Choose Add
Choose Resource Dictionary.
You can put your all your resource in this file.
Then add a resource file reference in App.xaml.
Dictionary1.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="txt" TargetType="TextBlock">
<Setter Property="Text" Value="asdf"/>
</Style>
</ResourceDictionary>
App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Page.xaml
<TextBlock Style="{StaticResource txt}" ></TextBlock>
I am using the MaterialDesign for XAML package in WPF. When I run my application, all styles and controls are rendered as expected. However in the XAML designer I have dozes of errors such as "The resource 'MaterialDesignFlatButton' could not be resolved." Example of a line that is throwing that error:
<Button Style="{StaticResource MaterialDesignFlatButton}" IsDefault="True" Margin="0 8 8 0" ...
My app.xaml contents is as follows:
<Application.Resources>
<ResourceDictionary>
<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.Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.LightBlue.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I have attempted the top solution proposed on The resource "x" could not be resolved. but that causes the project to fail to run (I believe I am not using the correct pathing when attempting to use the proposed "absolute pack URI"). So I have two questions at this point:
Is there a reason the resources would fail to resolve in the XAML designer given the way I have defined them in App.xaml (per the developer guide: https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/wiki/Getting-Started)?
How can I find out the "absolute pack URI" to use for the source for my resource dictionaries?
In the past, I had problems like this.
The error causes are as follow.
1. Setup and setting config
About this, please check the github and material design homepage.
2. Build and Compiler problem
About this, users may set the "Platform Target" as "x64".
That can invoke errors because material designer tool use "x32" compiler, so please use "any cpu" or "x32".
I had this problem with the flat accent button, while every other button style worked. I added the resource for buttons, then the error was gone. Then I removed the button resource... and the error was still gone.
https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/wiki/FAQ
Exception: Cannot find resource named 'MaterialDesign...'
This error typically comes when you have a static resource referencing
one of the material design styles, and have not included the
appropriate resource dictionary that contains the style. Try the
following:
Ensure that you have loaded all of the default material design styles in your App.xaml. You can find directions for this in the
Getting Started guide.
Ensure you have referenced the control specific resource dictionary that contains the style. The path for this is resource dictionary
should be .xaml" />. For example, if you were trying to reference the
MaterialDesignFloatingActionMiniButton style for a button, the
resource dictionary source would be: . Typically these inclusions are done at root of your Window, User
Control, or Template. You can find the full list of the resource
dictionaries here
I'm relatively new to WPF and I would like to add a loading indicator. I found https://github.com/100GPing100/LoadingIndicators.WPF which has some nice pre-made loading indicators and I installed the NuGet Package for it but I'm having a hard time implementing them to my window.
So I added
<Window ...
xmlns:loadin="clr-namespace:LoadingIndicators.WPF;assembly=LoadingIndicators.WPF"
...
>
Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/LoadingIndicators.WPF;component/Styles/LoadingDoubleBounce.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
But when I try to add one of the loading indicators to my grid I get an error on the dynamic resource
<Grid x:Name="TagLoadingIndicator" Panel.ZIndex="1">
<loadin:LoadingIndicator SpeedRatio="{Binding SpeedRatio}" IsActive="{Binding IsDoubleBounceActive}" Style="{DynamicResource LoadingIndicatorDoubleBounceStyle}"/>
</Grid>
The error I get is
The resource "LoadingIndicatorDoubleBounceStyle" could not be
resolved.
From what I saw in other websites, I am adding it to the ResourceDictionary correctly... And I'm assuming that by installing the NuGet package, I have the resource already defined. I can still run the application but the indicator is missing. What am I missing?
Thank you
That key exists in the Styles.xaml file. So add a reference to it in your resources.
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/LoadingIndicators.WPF;component/Styles/LoadingDoubleBounce.xaml"/>
<ResourceDictionary Source="pack://application:,,,/LoadingIndicators.WPF;component/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
I am having a nagging issue with my App.Resources MergedDictionary. Every time add a new dictionary with a source and XML namespace from another assembly, an error is produced and I cannot build my program.
The error appears in App.xaml with the ResourceDictionary underlined and the message Value Cannot be Null. Parameter Name: item. This makes absolutely no sense as I have done the exact same thing before. The only difference is that this time, I am referencing the namespace of another assembly(c# class library). Here is the App.xaml file:
<Application x:Class="Quiz.Client.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Quiz.Client"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles.xaml" />
<ResourceDictionary Source="Resources/Templates.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Below is Styles.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Quiz.Client">
<Style x:Key="StrongFont"
TargetType="ContentControl">
<Setter Property="FontSize"
Value="20" />
<Setter Property="FontWeight"
Value="ExtraBold" />
<Setter Property="Foreground"
Value="DarkRed" />
</Style>
</ResourceDictionary>
Here is Templates.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Quiz.Client"
xmlns:domain="clr-namespace:Quiz.Core.Domain;assembly=Quiz.Core"
xmlns:common="clr-namespace:Quiz.Client.Common">
<DataTemplate x:Key="ArithmeticQuestionTemplate"
DataType="domain:ArithmeticQuestion">
</DataTemplate>
<common:QuestionTemplateSelector x:Key="QuestionTemplateSelector"
ArithmeticTemplate="{StaticResource ArithmeticQuestionTemplate}" />
</ResourceDictionary>
What the heck is going on? Was there a software-breaking change introduced or something? I am completely lost.
Confirm!!! This bug is tracked by Microsoft. This mean WPF in some points will is trying to report to Microsoft automatically with Microsoft report process. During the report VS start freeze. Please, wait bug report to finish.
The bug still exist in lattes VS 2019.
After restart everything is fine. Confirm!!!
Thanks for the answer. I believe it is a Visual Studio bug caused by something unknown. Maybe one of my plugins, who knows. The only thing that fixes the issue is restarting visual studio and building the project. The problem seems to begin when adding a new namespace from another assembly. The project then can't build until visual studio is restarted. Very weird, never seen anything like it before. – user3096803
Not sure why your method isn't working but it's not how I usually import resources myself. Typically I'll add a ResourceDictionary to my library project, put all the libraries resources in that and import it into my main project using pack syntax:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyLibrary;component/LibraryResources.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- main project resources go here -->
</ResourceDictionary>
In general I'd argue that this is a better architecture anyway as it doesn't require a re-compile of your main project each time one of the child library resources change. It also makes it easier later on if you decide to switch to MEF, as your library resources are now encapsulated in a single ResourceDictionary object that you can readily [Export].
I don't really understand the answer from #Pit but I tried restarting visual studios and also changing all source paths to full pack URIs, and nothing seemed to work. Came across this thread that hinted at using Tools > Options > Preview Features > New WPF XAML Designer for .NET Framework which fixed the issue for me.
EDIT: This "fix" may have been a red herring, it still had issues but they were equally cryptic! After switching back to the old XAML designer and doing some further digging I found that one of my merged dictionaries was incompatible with the wpf designer and it was resulting in this error. The fix on that page resolved my issue.
This specific fix probably won't help anyone, but hopefully the methodology will. Check if there are any errors in any of the individual resource dictionaries in the merged dictionaries.
Case
I've got two assemblies: one holding the application, one acting as library.
The library holds and image resource, say: "Images/image.png" with its build action set to resource.
Library
Next to that, the library contains a SomeStyle.xaml file:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Image x:Key="someKey" Source="/LibraryAssemblyHere;component/Images/image.png" />
</ResourceDictionary>
Application
The application assembly has the App.xaml with:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/LibraryAssemblyHere;component/Styles/SomeStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Problem
When starting the application, Visual Studio 2010 gives me the following error:
Failed to create a 'ImageSource' from the text '/LibraryAssemblyHere;component/Images/image.png
With an inner exception:
Cannot locate resource 'images/image.png'.
Question
Simple: What am I doing wrong? I've searched and searched, but nothing helped.
Thanks in advance!