Application.Resources availability during Bootstrapping

Aug 1, 2008 at 4:22 PM
I'm trying to get a reference to a resource that is included through Application.Resources MergedDictionaries in my App.xaml. When during the bootstrapping process are those resources available? In a test WPF application w/out Prism I can access them in the constructor of my main window, but with Prism they are not available in the Shell constructor.
Aug 1, 2008 at 7:41 PM

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.

 

 

Aug 8, 2008 at 6:50 PM
I think the problem is that "InitializeComponent()" call is missing at the CompositeWPF walkthroughs and documentation related to bootstrappers. The samples there give the empty App() constructor where the Bootstrapper is instantiated. I'm afraid there might be a lot of similar discussion topics untill the documentation/samples are corrected.

Best regards, Denis
Aug 13, 2008 at 12:47 AM
I had the same problem, the Window instances referring to the styles would appear unstyled if they were created and shown prior to the main shell window being shown, once the main window is shown the window would flash as you could see the styles being applied after it was created. I resolved this by moving all the window creation into the App.OnStartup(StartupEventArgs) virtual method. You can also add the InitializeComponent call to your constructor, just make sure it's before any calls to present UI, as far as I'm aware this call is added to the constructor automatically.

-Brett
Aug 22, 2008 at 1:13 AM
Edited Aug 22, 2008 at 1:23 AM
Wow! Wish I'd found this earlier as I was tearing my hair out building my spike on the bones of the UIComposition QuickStart! You don't want to know all the stupid things I tried.
Combining @DenisVuyka and @brettryan, I do as shown below. It seems to work. Anyone think I should do otherwise? Am I being paranoid delaying BootStrapper until "OnStartup"?

public partial class App : Application {
public App() {
InitializeComponent();
}

protected override void OnStartup(StartupEventArgs e) {
new BootStrapper().Run();
}
}

 

Oct 3, 2008 at 5:00 PM
Edited Oct 3, 2008 at 5:01 PM

Hi,

 

We have just published an article regarding this issue in the Known Issues / Fixes section of Composite WPF KB:

·          Bootstrapper initialization is not at the Application Startup event in some of the Quickstarts and Documentation topics

 

Ignacio Baumann Fonay

http://blogs.southworks.net/ibaumann/

Jan 9, 2014 at 4:55 PM
Edited Jan 9, 2014 at 4:58 PM
I just encountered an Interesting behavior creating a brand new application. "The name 'InitializeComponent' does not exist in the current context" happened after I added the following to see if it would resolve my issue of not being able to access App.xaml styles from my modules:
        public App()
        {
            InitializeComponent();
        }
I found this odd as I had not encountered this problem until now - when I discovered the problem it made sense as I generally have merged dictionaries. In your App.Xaml file you have to have the following: <ResourceDictionary.MergedDictionaries/>

With this statement you no longer require the constructor or InitializeComponent (and it works).

Example follows:
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries/>
            <Style TargetType="Button">
                <Setter Property="Command" Value="{Binding GenericCommand}"/>
                <Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource Self}}"/>
            </Style>            
        </ResourceDictionary>
    </Application.Resources>