Organizing Xaml - Unable to apply StaticResource from different xaml file - c#

I have a situation where the wpf application is not able to pickup StaticResource and instead fails with XamlParseException. But if I used a DynamicResource instead, the resource is found and no exception occurs.
I was trying to style and organize wpf resources as recommended at http://projekt202.com/blog/2010/xaml-organization/
I have 2 projects accordingly, a wpf control library that houses all resources and a main wpf project which uses those resources. Here is the structure of the 2 projects
Projects Structure
Wpf_Theme.ControlLibrary
--ResourceDictionaries
----BaseControlStyles
------ButtonStyle.xaml
------TextBoxStyle.xaml
----Brushes
------DefaultBlueTheme
----ResourceLibrary.xaml
Wpf_Theme.Main
--App.xaml
--MainWindow.xaml
Contents of xaml files
ButtonStyle.xaml
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="{StaticResource ControlBackground}"/>
<Setter Property="BorderBrush" Value="{StaticResource BorderColor}"/>
...
</Style>
DefaultBlueTheme.xaml (Brushes)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="PanelBackground" Color="#C8DCF0"/>
<SolidColorBrush x:Key="BorderColor" Color="#6A8FB5"/>
<SolidColorBrush x:Key="SelectedItemBackground" Color="Wheat"/>
<SolidColorBrush x:Key="TextForeground" Color="Black"/>
<LinearGradientBrush x:Key="ControlBackground" StartPoint="0,0" EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0" Color="#DBECFD"/>
<GradientStop Offset="0.5" Color="#C7DBEF"/>
<GradientStop Offset="1" Color="#B0CAE5"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</ResourceDictionary>
ResourceLibrary.xaml (Merges all dictionaries in one file to be used by main project)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Brushes/DefaultBlueTheme.xaml"/>
<ResourceDictionary Source="BaseControlStyles/ButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
App.xaml (In Main project)
<Application x:Class="Wpf_Themes.Main.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/Wpf_Themes.ControlLibrary;component/ResourceDictionaries/ResourceLibrary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
MainWindow.xaml
<Window x:Class="Wpf_Themes.Main.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel Width="200" Margin="10">
<Button>Click Me</Button>
</StackPanel>
</Window>
As stated earlier in the post, I am not able to resolve the style(Background and Border brushes) for the Button using the StaticResource applied in ButtonStyle.xaml. If I use DynamicResource instead, the brushes are found correctly and applied to the Button. Any insights why this behavior occurs.
Edit:
Following Mike's suggestion, I included the xaml files from the Wpf_Theme.ControlLibrary project directly into the App.xaml of the Main project like below
App.xaml
<Application x:Class="Wpf_Themes.Main.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Wpf_Themes.ControlLibrary;component/ResourceDictionaries/Brushes/DefaultBlueTheme.xaml"/>
<ResourceDictionary Source="/Wpf_Themes.ControlLibrary;component/ResourceDictionaries/BaseControlStyles/ButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
and resources are now located correctly using the StaticResource

Static resource references are resolved at parse time (for the most part), whereas dynamic references are resolved at run time. This means a static reference can only be used if the resource has been parsed before the reference, whereas a dynamic reference can be used as a forward reference for a resource that is defined later.
Source: http://drwpf.com/blog/category/resources/
When the two ResourceDictionaries are in a separte Assembly and you reference them at the same time I would guess that this processing happens at the same time. Whereas if you load them in App.xaml directly it is ensured that they are loaded in the right order.
So the Resources of your first Dictionary are not available to the second Dictionary since you include them in the same Dictionary.
There is two ways to solve the Problem. Either you use DynamicResources which are evalutated at runtime (like you already tried).
Another solution if you now the hyrachie of your Resource Dictionaries you can do several levels. Like:
<Application x:Class="WPF_Theme.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/ControlLibrary;component/ResourceDictionaries/BaseLevel.xaml" />
<ResourceDictionary Source="pack://application:,,,/ControlLibrary;component/ResourceDictionaries/SecondLevel.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
BaseLevel.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Brushes/DefaultBlueTheme.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
SecondLevel.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="BaseControlStyles/ButtonStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
DefaultBlueTheme.xaml and ButtonStyle.xaml stays unchanged.
With this you would ensure that the different ResourceDictionaries are already there if you need them.
I hope that helps.

Related

UWP: application wide TextBlock foreground crashes app when set from static resource

I went through a lot of similar questions on SO, in regards to setting the foreground of a TextBlock through a static resource, but for whatever reason my UWP application keeps crashing on me.
I am starting to believe this is a limitation of UWP? Also, no stacktrace is generated from this.
I am using Microsoft Visual Studio Community 2022 v.17.2.6
ThemeColors.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="MyOffWhiteBrush" Color="#eeeeee" />
</ResourceDictionary>
TextBlockStyle.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="{StaticResource MyOffWhiteBrush}" />
</Style>
</ResourceDictionary>
App.xaml
<Application
x:Class="TestTextBlock.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestTextBlock">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ThemeColors.xaml" />
<ResourceDictionary Source="TextBlockStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
MainPage.xaml
<Page
x:Class="TestTextBlock.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestTextBlock"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<TextBlock Text="helloworld" />
</Grid>
</Page>
UPDATE #1:
If I change TextBlockStyle.xaml to this it works.
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="_" />
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="{StaticResource MyOffWhiteBrush}" />
</Style>
</ResourceDictionary>
I don't understand why I need to declare an empty brush?
The reason for this behavior may be related to the Lookup behavior for XAML resource references and Forward references within a ResourceDictionary. You need to make sure the referenced resource must appear lexically before the resource reference when you use the resource.
I agree with #Mark W, the possible workaround is that you could define the basic resource on top of other resources.
In your scenario, it should be like this:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ThemeColors.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="{StaticResource MyOffWhiteBrush}" />
</Style>
I don't have the answer for why... In my application, I have a number of styles - separated out into color/brushes, sizes and layouts, and then default styles... for each dictionary that references other dictionaries, I include them at the top.
ColorsAndBrushes does not have any references.
SizesAndLayout does not have any references.
DefaultStyles references the two above.
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ColorsAndBrushes.xaml" />
<ResourceDictionary Source="SizesAndLayout.xaml" />
</ResourceDictionary.MergedDictionaries>
so for my next layer, "NamedStyles", I have have the following to the top:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ColorsAndBrushes.xaml" />
<ResourceDictionary Source="SizesAndLayout.xaml" />
<ResourceDictionary Source="DefaultStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
I don't know if this will work better for you, but maybe feels a little better than the random empty brush. And my app.xaml references all of them. This was build on a older (AnniversaryEdition) UWP SDK, so your mileage may vary.
Good luck!
-Mark

Unable to resolve resources using separate files

I feel like this is a common sense and trivial, but i don't understand what i'm doing to begin with. I don't have any other resource I can use for help either. Sometimes i wonder if I'm even googling the question right.
I have some custom styles & templates I've made, but now the file is rather large and difficult to work with. I want to put each style or template in there own XAML files (sorta like headers/implementation files) so that a friend could work on one and then we add it to the project. (Such as Dictionary1.xaml ... ). I started a blank project to keep it simple.
Dictionary1.XAML
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary x:Key="SomeKey">
<Color x:Key="detailMark">Black</Color>
<SolidColorBrush x:Key="detailMarkBrush" Color="{StaticResource ResourceKey=detailMark}" />
<Style x:Key="flatTextBox" TargetType="{x:Type TextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<Rectangle Stroke="{StaticResource ResourceKey=detailMarkBrush}" StrokeThickness="1"/>
<TextBox Margin="1" Text="{TemplateBinding Text}" BorderThickness="0"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
App.XAML
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
StartupUri="MainWindow.xaml">
<Application.Resources>
<local:MainWindow x:Key="SomeKey"/>
</Application.Resources>
</Application>
And MainWindow.XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns:local="clr-namespace:WpfApplication1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"
Height="350" Width="525">
<Grid>
<TextBox Style="{DynamicResource flatTextBox}"> <!-- doesn't autocomplete/work -->
</TextBox>
</Grid>
</Window>
Edit:
<TextBox Style="{Binding Mode=OneWay, Source={StaticResource SomeKey}}">
<!-- Throws System.Windows.Markup.XamlParseException -->
</TextBox>
As Alex mentioned, the right way to do this is using Merged Dictionaries.
Therefore, you should structure your project correctly, otherwise it will end up in a mess.
Keeping your "blank project", it should look like this:
YourProject
App.xaml (.cs)
MainWindow.xaml (.cs)
SomeOtherWindow.xaml (.cs)
Resources folder
Dictionary1.xaml
Dictionary2.xaml
...
Then you have to decide:
Do you want the resources to be available application wide?
Or do you want the resources to vary between certain windows / user controls?
If you want #1, you have to merge the dictionaries in the App.xaml file:
<Application x:Class=...
...>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Dictionary1.xaml"/>
<ResourceDictionary Source="Resources/Dictionary2.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
If you want #2, you have to merge the dictionaries in the specific window / user control file:
<Window x:Class=...
...>
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Dictionary1.xaml"/>
<ResourceDictionary>
<!-- Window specific resources -->
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<!-- Content -->
</Window>

Load control style from separate file in wpf

I have the following style added to my Windows.Resources
<Window.Resources>
...
<!--A Style that extends the previous TextBlock Style-->
<!--This is a "named style" with an x:Key of TitleText-->
<Style BasedOn="{StaticResource {x:Type TextBlock}}"
TargetType="TextBlock"
x:Key="TitleText">
<Setter Property="FontSize" Value="26"/>
<Setter Property="Foreground">
<Setter.Value>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.0" Color="#90DDDD" />
<GradientStop Offset="1.0" Color="#5BFFFF" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
...
</Window.Resources>
I have a lot of those styles in my xaml code and I would like to save each component style to an extra file (not an external file).. for example all the styles related to TextBlocks should be in a file called TextBlockStyles.xaml
How would I do this in wpf?
How do I link the style in my project ?
Thanks in advance
You use merged resource dictionaries
In you app.xaml you would use
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/Your.Assembly.Name;component/TextBlockStyles.xaml"/>
... other dictionaries here
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
or directly into a UserControl would be
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/Your.Assembly.Name;component/TextBlockStyles.xaml"/>
... other dictionaries here
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
You can shorten Source="..." to just Source="TextBlockStyles.xaml" if the file is in the same assembly and in the root of the project, or alternatively Source="Styles\TextBlockStyles.xaml" if you put the resource dictionary into the folder Styles.
Use case: you have a user control called MyView.xaml with a button. You want to style the button with an external XAML file.
In MyView.xaml:
<User Control ...namespaces...>
<UserControl.Resources>
<ResourceDictionary>
...converters...
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyButton.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
...the rest of the control...
</UserControl>
In MyButton.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MSDNSample">
<Style x:Key="FooButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Pink" />
</Style>
</ResourceDictionary>
Back to MyView.xaml ("the rest of the control"):
<Button Style="{StaticResource FooButton}">
Hello World
</Button>
In Solution Explorer Right Click on your Project Select Add After that click on Resource Dictionary...
Choose name and add to your project.
Open App.xaml
Add This Code in Application Tag
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="YourStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
In YourStyle.xaml :
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:APPNAME">
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Pink" />
</Style>
</ResourceDictionary>
you are looking for the dynamic resources. well the best way is to load and marge the dictionary in the resources. application or either on control page.
here is a good sample for it.
http://blogs.msdn.com/b/wpfsdk/archive/2007/06/08/defining-and-using-shared-resources-in-a-custom-control-library.aspx
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
this.Resources.MergedDictionaries.Add(Smyresourcedictionary);
Simply, go to your Window (example: MaindWindow.xaml) where you want to include the resource from the outer file and use MergedDictionaries principle to refer to that file:
<Window x:Class="UseMergedResource.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="600"
Width="600">
<Window.Resources>
<!-- DECLARING MERGED DICTIONARY -->
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source='Merged/BrushResources.xaml' />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<!---------------------------------->
</Window.Resources>
<StackPanel>
<Rectangle Width='200'
Height='100'
Fill='{StaticResource PrimaryBrush}' /> <!-- USAGE HERE -->
</StackPanel>
</Window>
From above Merged/BrushResources.xaml is the location of the resource file, which is located under the folder called Merged.
Now if you are wondering what should be the declaration syntax in the outer file, check this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Location for application brushes -->
<SolidColorBrush x:Key='BorderBrush'
Color='Orange' />
<SolidColorBrush x:Key='HighLightBrush'
Color='LightBlue' />
<SolidColorBrush x:Key='PrimaryBrush'
Color='Pink' />
<SolidColorBrush x:Key='AccentBrush'
Color='Yellow' />
</ResourceDictionary>
If you want to make the resource available through all application (visible in all your windows) then declare in the App.xaml resources section.

Converting WPF local resource to application resource

I have a local resource in XAML and I want it to be used by other parts of the code. How can I make it "global" (i.e. an application-wide resource)? Here is my local resource:
<ResourceDictionary >
<local:BoolToLightConvertor x:Key="LightConverter" / >
</ResourceDictionary>
How can I put this in the App.xaml?
Application Resources
In addition to defining resources at the level of the element or Window, you can define
resources that are accessible by all objects in a particular application. You can create an application
resource by opening the App.xaml file (for C# projects) or the Application.xaml file
(for Visual Basic projects) and adding the resource to the Application.Resources collection, as
shown here:
<Application x:Class="WpfApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<SolidColorBrush x:Key="appBrush" Color="LightConverter" />
</Application.Resources>
</Application>
Create a file (e.g. SharedResources.xaml) as follows:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Your.Namespace.Here;assembly=Your.Assembly.Here">
< local:BoolToLightConvertor x:Key="LightConverter" / >
</ResourceDictionary>
In your App.xaml add the following lines:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="SharedResources.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Rectangle}" />
</ResourceDictionary>
</Application.Resources>
You can now use this converter from within the XAML
(The <Style TargetType="{x:Type Rectangle}"/> is a workaround to stop a WPF bug ignoring your resource dictionary and was recommended by another question here on SO. The link unfortunately eludes me now though)

Referencing a resource in a ResourceDictionary from a different ResourceDictionary in Silverlight

I have the following set of code in my App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Client.Common;component/Theme/Brushes.xaml"/>
<ResourceDictionary Source="/Client.Common;component/Theme/Fonts.xaml"/>
<ResourceDictionary Source="/Client.Common;component/Theme/CoreStyles.xaml"/>
<ResourceDictionary Source="/Client.Common;component/Theme/SdkStyles.xaml"/>
<ResourceDictionary Source="/Client.Common;component/Theme/MyAppName.xaml"/>
<ResourceDictionary Source="/Client.Common;component/Controls/NavigationPanel.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
The NavigationPanel.xaml contains a style that looks like this:
<Style x:Key="NavigationPanelListBox" TargetType="ListBox">
<Setter Property="Background" Value="{StaticResource DarkBackground}" />
<Lots of XAML>
</Style>
The {StaticResource DarkBackground} is defined in the Brushes.xaml file (i.e. the first resource dictionary). It is defined as
<SolidColorBrush x:Key="DarkBackground" Color="#FF707176" />
in the resource dictionary.
At runtime, I get the following error:
Cannot find a Resource with the Name/Key DarkBackground [Line: 16 Position: 44]
The line numbers and position references the NavigationPanel.xaml resource dictionary in the app.xaml.
I can reference the brush from other controls, just not the included resource dictionary.
Why can I not reference or why does it not resolve the reference to a resource that is higher in the heirarchy of the merged resource dictionary?? What am I missing here?
Are you referencing the DarkBackground brush in any of the resources in the NavigationPanel dictionary?
If you are you might need to merge the Brushes resource dictionary into the NavigationPanel dictionary.
So in the NavigationPanel dictionary.
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Client.Common;component/Theme/Brushes.xaml" />
</ResourceDictionary.MergedDictionaries>
You can include one dictionary in another (like 'using' in C#) like so:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:uriMapper="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Controls="clr-namespace:APC.IKM.UI.SL.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Brushes.xaml"/>
<ResourceDictionary Source="Fonts.xaml"/>
<ResourceDictionary Source="CoreStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
Is this what you are looking for? The Cosmopolitan / Metro project template has a good example of this...
the trully answer is Eric's answer in this site: https://social.msdn.microsoft.com/forums/windowsapps/en-US/2be9a5f6-5313-448d-a9d9-296bac42215e/using-style-defined-in-merged-dictionary-from-another-merged-dictionary?forum=wpdevelop.
the Brushes.xaml and the NavigationPanel.xaml is parsed independently and then added to the merged dictionary of Application resources so they don't know anything about each other.

Categories