In the following message Application.Resources availability during Bootstrapping srccoder has an issue programmatically accessing the resource dictionary from the application and/or shell constructor. I provided the following response and example of how it can (and does) work.
Can you provide an example of what is not working? I'll provide an example of what does :) Using the CompositeWPF Commanding solution...
I added the following to the Styles/Styles.xaml
<Style x:Key="MyLabel" TargetType="{x:Type Label}">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</Style>
The App.xaml file looks as follows (in its entirety)
<Application x:Class="Commanding.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles\styles.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
App.xaml.cs - code behind
public partial class App : Application
{
public App()
{
InitializeComponent(); // <== Must execute before FindResource in main app
Style myLabelStyle = FindResource("MyLabel") as Style;
if (myLabelStyle == null)
throw new Exception("MyLabel style not found");
}
....
}
Shell.xaml.cs - code behind
public Shell()
{
Style myLabelStyle = FindResource("MyLabel") as Style;
if (myLabelStyle == null)
throw new Exception("MyLabel style not found");
InitializeComponent();
}
Everything runs without error. I added the following label to the bottom of the Shell.xaml file:
<Label HorizontalAlignment="Right" Content="Hello" Style="{DynamicResource MyLabel}" Width="87" />
<Separator Margin="0,0,0,0" VerticalAlignment="Bottom" Height="10" BorderBrush="#193441"/>
</Grid>
</Window>
It works just fine whether I use StaticResource or DynamicResource.
Tags:
compositewpf,
resourcedictionary,
styles
Categories:
CompositeWPF