.netwpfdynamicresource

WPF Dynamic Resource from current project


I have two separate WPF projects, each with their own defined Dynamic Resources which mostly defines colors in each application. The task at hand is to include the main pages from each project in a new project. So far the projects are both included, but the Dynamic Resources are used from the new project.

Can I make sure that pages from each project will use the Dynamic Resources from their own project, instead of using the resources from the executing project?

Example of the issue:

The GreenPage.xaml and application.xaml from the old application:

<Page x:Class="GreenPage"
      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" 
      xmlns:local="clr-namespace:Test"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="GreenPage">

    <Grid Background="{DynamicResource DefinedColor}">

    </Grid>
</Page>
<Application x:Class="Application"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Test"
    StartupUri="MainWindow.xaml">
    <Application.Resources>
        <SolidColorBrush x:Key="DefinedColor" Color="Green"/>
    </Application.Resources>
</Application>

The GreenPage is green when I expect it in the designer, however, when inserting the page in a window in another project, the background is not green anymore:

<Window x:Class="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"
        xmlns:local="clr-namespace:NewProject"
        xmlns:test="clr-namespace:Test;assembly=Test"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <test:GreenPage/>

</Window>

Solution

  • Can I make sure that pages from each project will use the Dynamic Resources from their own project, instead of using the resources from the executing project?

    Not if you define the resources in App.xaml. There is no concept of a "project" at runtime. There is only executable application running and what resources are being applied depends on what resources that are in scope at runtime in this particular application.

    So if you define a Brush resource directly in the App.xaml file of app A, this resource will never be in scope or applied when you run any other application.

    I assumed the resources are used from the project which defines the page

    This is not true. Again, there is no concept of a project at runtime and the window has no connection to the actual App.xaml file. The resource is looked up at runtime.

    If you want a Page in project A to always have a specific colour, it would make more sense to define the resource directly in the Page class instead of defining it in App.xaml. Or don't use a resource at all.