Firoso
Firoso

Reputation: 6685

StaticResource in app.xaml not found in Resource Dictionary referenced from app.xaml

I've got a situation where I'm attempting to resolve visual components in a data template referenced in a resource dictionary, referring to a static resource of the app.xaml markup, see example below.

App.xaml

<Application 
    x:Class="App" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"      
    xmlns:composition="clr-namespace:Composition"
    ShutdownMode="OnMainWindowClose">
    <Application.Resources>
        <ResourceDictionary>
            <composition:ApplicationContainer x:Key="ApplicationContainer"/>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary
Source="pack://application:,,,/MyApp;component/Composition/DataTemplates.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Composition/DataTemplates.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:projectvm="clr-namespace:ViewModels.ProjectManagement"
                    xmlns:unity="clr-namespace:Extensions">
    <DataTemplate DataType="{x:Type projectvm:ProjectDocument}">
        <ContentControl>
            <!-- Custom Extension that resolves a component from
            the composition container -->
            <unity:Resolve Container="{StaticResource ApplicationContainer}"
  TargetType="{x:Type projectvm:ProjectDocument}" ContractName="ProjectDocument"/>
        </ContentControl>
    </DataTemplate>
</ResourceDictionary> 

The error is just above. The unity: Resolve line cannot locate the ApplicationContainer Static Resource. And I don't understand why.

Upvotes: 6

Views: 1227

Answers (1)

Sam
Sam

Reputation: 1

One note that I've found helped fix this issue when I ran into it was moving App.xaml into the root of the project. Previously I had moved App.xaml into a subfolder, and the Designer could no longer locate any StaticResource references. Moving App.xaml up into the root fixed all these issues.

Not sure why this helped, but this answer has more details on how the Designer finds resources.

Upvotes: 0

Related Questions