I have a ResourceDictionary included in my Application.Resources area of my WPF project. it
From App.xaml (in the order of this SO answer ):
App.xaml:
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="MyDictionary.xaml"> </ResourceDictionary> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>
In this dictionary, I have several styles, including the page style:
MyDictionary.xaml:
<SolidColorBrush x:Key="PageBackgroundBrush" Color="Black" /> <Style x:Key="PageStyle" TargetType="Page"> <Setter Property="Background" Value="{StaticResource ResourceKey=PageBackgroundBrush}" /> <Setter Property="Height" Value="Auto" /> <Setter Property="Width" Value="Auto" /> </Style>
MyPage.xaml:
<Page x:Class="MyProject.MyPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="250" Title="Page" Style="{StaticResource ResourceKey=PageStyle}" > <Grid> </Grid> </Page>
MyPage.xaml.cs
public class MyPage : Page { public MyPage() {
This setting works very well when Visual Studio views the page in design mode and when the project starts.
I am using Visual Studio built in Unit Test tools . When I try to run unit tests for the MyPage class (which uses MyPage.xaml when running the constructor), my tests fail. MyPage.xaml uses the styles defined in the dictionary included in Application.Resources. Tests do not recognize the PageStyle parameter because Application.Resources did not turn on when starting Unit Test, and as a result, the page was not created. How to enable my applications. Resources in my unit tests? Also, is there a better way to run unit tests for WPF pages and windows?
source share