在 app.xaml 引用的资源字典中找不到 app.xaml 中的静态资源

问题描述 投票:0回答:1

我遇到了一种情况,我试图解析资源字典中引用的数据模板中的可视组件,引用

app.xaml
标记的静态资源,请参阅下面的示例。

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> 

错误就在上面。统一:解析行无法找到

ApplicationContainer
静态资源。我也不明白为什么。

.net wpf staticresource
1个回答
0
投票

当我遇到这个问题时,我发现有助于解决这个问题的一个注释是将 App.xaml 移动到项目的根目录中。以前,我已将 App.xaml 移至子文件夹中,设计器无法再找到任何 StaticResource 引用。将 App.xaml 移至根目录修复了所有这些问题。

不知道为什么这有帮助,但是这个答案有更多关于设计师如何查找资源的详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.