-1

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>
  • Which resouces will be used in a scope at runtime depends on the order in which they are loaded. Please clarify your issue. – mm8 Dec 03 '19 at 14:31
  • @mm8 I have tried to update my question with a similar example, hopefully, it will clarify the issue. – Morten Kristensen Dec 03 '19 at 14:51
  • Why would the window be green when you open it an application that doesn't define any such `DefinedColor` resource? How do you expect/think your resources are applied? – mm8 Dec 03 '19 at 15:01
  • I assumed the resources are used from the project which defines the page. – Morten Kristensen Dec 03 '19 at 15:04

1 Answers1

1

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.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Alright, it makes good sense. The reason why the colors were placed in the app.xml is that the colors are used in multiple pages, hence making it easy to change the colors for the entire application from the single file. Can I keep this functionality in another manner? – Morten Kristensen Dec 03 '19 at 15:19
  • If you put them in a resource dictionary, you could load this resource dictionary into the running app either programmatically or using XAML markup depending on your requirements. – mm8 Dec 03 '19 at 15:20
  • Okay, that sounds like like the answer I was looking for. Thank you! Much appreciated :) – Morten Kristensen Dec 03 '19 at 15:44