WPF - 资源未从 Generic.xaml 加载

发布于 2024-10-31 20:10:34 字数 1227 浏览 1 评论 0原文

Themes\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>

Controls\Layout\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 中,它仍然不起作用。

Themes\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>

Controls\Layout\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>


If I copy the entire style into my usercontrol resources it works fine. But, if I don't, the usercontrol shows up empty. In Expression Blend 4, I right-clicked and chose Edit Template>, but it won't let me select Edit a Copy... which leads me to believe that something is severely wrong and the Generic.xaml isn't loading properly. I figure it's Generic.xaml because if I remove the MergedDictionary call and copy/paste the xaml style directly into Generic.xaml it still doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无可置疑 2024-11-07 20:10:34

我会大胆猜测您更改了 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 的信息。 :)

I'm gonna take a wild guess that you altered your AssemblyInfo.cs file and either changed (or removed) the following line:

[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)
)]

You need to tell your assembly about your ThemeInfo. :)

撕心裂肺的伤痛 2024-11-07 20:10:34

从我的博客复制:

在您需要的 Properties\AssemblyInfo.cs 的开头(请注意,这在 Silverlight 中不使用/不需要):
使用系统.Windows;

...

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

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

copying from my blog: http://zoomicon.wordpress.com/2012/06/10/what-to-do-if-generic-xaml-doesnt-get-loaded-for-wpf-control/

at the start of Properties\AssemblyInfo.cs you need (note this isn’t used/needed in Silverlight):
using System.Windows;

...

Mind you that if the project doesn’t show a Properties node in Solution Explorer, you have to either make a new project using the correct template (for a WPF custom control), or right click the project, select Properties, then press the Assembly Information button and enter some dummy values, then OK to create the Properties node (which also creates to a Properties subfolder and AssemblyInfo.cs file).

You can expand (drop-down) the special Properties node in solution explorer then to open AssemblyInfo.cs and add the above stuff if missing

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文