【问题标题】:WPF - Resource not loading from Generic.xamlWPF - 资源未从 Generic.xaml 加载
【发布时间】:2011-08-04 02:03:15
【问题描述】:

主题\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 中,我右键单击并选择了 Edit Template>,但它不允许我选择 Edit a Copy...,这让我相信某些事情严重错误并且 Generic.xaml 没有正确加载。我认为它是 Generic.xaml,因为如果我删除 MergedDictionary 调用并将 xaml 样式直接复制/粘贴到 Generic.xaml 中,它仍然不起作用。

【问题讨论】:

  • 你能展示你的FooItem代码吗?通常,不应重新模板化 UserControls(即设置 Template 属性),因为它不能按预期工作。
  • FooItem.cs 暂时是空的,我想确保布局正确,稍后慢慢添加其他 DependencyProperties 和逻辑。
  • FooItem 从何而来?
  • 这是因为我试图改变 ThemeInfo 的东西,就像 myermian 说的那样。不过还是谢谢。

标签: wpf custom-controls resourcedictionary generic.xaml


【解决方案1】:

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

【讨论】:

  • 遇到同样的问题并使用上述属性修复。在我的情况下,问题是由最初未设置为 WPF 自定义控件库的 c# 库项目引起的(因此上述属性不会自动添加到程序集中),然后添加自定义控件
【解决方案2】:

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

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

...

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

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

【讨论】:

  • 我也刚刚注意到,如果您在用于控件样式的资源的 XAML 定义中有一个键,它将不会加载,您应该只使用 Target 属性 - 这可能会创建内部一些隐式键
猜你喜欢
  • 2018-09-08
  • 2011-12-09
  • 2023-03-15
  • 1970-01-01
  • 2011-12-07
  • 2016-12-16
  • 1970-01-01
  • 2017-10-09
  • 1970-01-01
相关资源
最近更新 更多