0
votes

I have a solution with three projects. One is for Testing using CodedUI, the other is a WPF Viewer application that shows the data from the tests, and the third is a project of Models.

If I set the WPF project to be the start up project in the solution and run it in debug mode the theme shows up just fine.

If I attempt to start the WPF MainWindow from the Test Project, everything shows the same, without the Theme. When the test project wants to show the data it does so by firing an event to this method in the WPF project.

public static  class Start
{
    public static EventHandler<List<SomeData>> NewData = ShowData;
    private static void ShowData(object sender, List<SomeData> e)
    {
        MainWindow.NewData(sender, e);
        var mw = new MainWindow();
        mw.ShowDialog();
     }
}

I have single stepped through this code, the data arriving and the mainwindow coming up just fine. But why wouldn't the code above include the theme?

There are no loading errors in output window.

The App.XAML is where the reference to the theme is located. But it is also where the StartupURI is located which is MainWindow.XAML. Do I need to somehow start App.XAML to get the theme?

Please advise...

1

1 Answers

0
votes

How to start a WPF application and ensure the Theme works! (given the situation mentioned above)

        var app = new App();        
        app.InitializeComponent();
        app.Run();  

Where App is your WPF application found here ==> App.xaml/App.cs and App.gs.i.cs. The InitializeComponent call was the key. In the gs file this line is does the magic.

System.Windows.Application.LoadComponent(this, resourceLocater);

Notice the "this" keyword. It is a reference to the App class itself which in the XAML have these "references/attributes/resources"

<Application x:Class="AuomatedTest.Viewers.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="Themes/ExpressionDark.xaml"></ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

The Theme itself was located in the project with these build attributes:

enter image description here

The bin directory had no folder named "Themes" in it; rather, it was the obj folder that contained the Theme; however, it was the "compiled" version of the file denoted by the .baml extension.

enter image description here

This means that the Page attribute of the XAML file compiles it. But what it doesn't tell us is how a reference like this is adequate.

<ResourceDictionary Source="Themes/ExpressionDark.xaml">

Today we know that without the Application.LoadComponent being called from the place we referred to the Theme (App.Xaml)that no Theme will be displayed. LoadComponent then knows how to interpret .BAML files and treat them as if they actually existed in the location that was referenced.