WPF - 无法从Generic.xaml加载资源

16

主题\Generic.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="WPF Commons;component/Controls/Layout/Foo/FooItem.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

控件\布局\Foo\FooItem.xaml:

<Style TargetType="{x:Type l:FooItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type l:FooItem}">
                <Border>
                    <ContentPresenter ContentSource="Header" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

如果我将整个样式复制到我的用户控件资源中,它可以正常工作。但如果不这样做,则用户控件将显示为空。在Expression Blend 4中,我右键单击并选择编辑模板,但它不会让我选择编辑副本...,这让我认为出现了严重的问题,Generic.xaml没有正确加载。我猜测是因为 Generic.xaml 的问题,因为如果我删除 MergedDictionary 调用并将 xaml 样式直接复制粘贴到 Generic.xaml 中,它仍然无法工作。


你能展示一下FooItem的代码吗?通常情况下,不应该重新模板化UserControls(即设置Template属性),因为它不会按预期工作。 - CodeNaked
FooItem.cs目前为空,我想确保我得到了正确的布局,并逐步添加其他DependencyProperties和逻辑。 - michael
FooItem继承自哪里? - CodeNaked
是因为我试图像myermian说的那样改变ThemeInfo的东西。不过还是谢谢。 - michael
2个回答

31

我猜测你修改了 AssemblyInfo.cs 文件,并且可能改变或删除了以下代码行:

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
    //(used if a resource is not found in the page, 
    // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
    //(used if a resource is not found in the page, 
    // app, or any theme specific resource dictionaries)
)]

你需要告诉你的汇编关于你的ThemeInfo信息。:)


3
遇到同样的问题并使用上述属性进行修复。在我的情况下,问题是由于采用一个原本未设置为WPF自定义控件库的C#库项目(因此上述属性未自动添加到程序集中),后来添加了自定义控件所导致的。 - cordialgerm

1

从我的博客复制:http://zoomicon.wordpress.com/2012/06/10/what-to-do-if-generic-xaml-doesnt-get-loaded-for-wpf-control/

在 Properties\AssemblyInfo.cs 的开头,你需要添加以下代码(请注意,这不适用于 Silverlight): using System.Windows;

...

请注意,如果项目在“解决方案资源管理器”中没有显示“属性”节点,则必须使用正确的模板(用于 WPF 自定义控件)创建一个新项目,或右键单击项目,选择“属性”,然后按“程序集信息”按钮并输入一些虚拟值,然后单击“确定”以创建“属性”节点(这也会创建一个“属性”子文件夹和 AssemblyInfo.cs 文件)。

您可以展开(下拉)解决方案资源管理器中的特殊“属性”节点,然后打开 AssemblyInfo.cs 并添加上述缺失的内容。


我也刚注意到,如果您在控件样式的资源的XAML定义中有一个键,它将无法加载,您应该只使用Target属性 - 这可能会在内部创建一些隐式键。 - George Birbilis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接