ComponentResourceKey 作为 DynamicResource 问题

2024-05-01

我在使用 ComponentResourceKey 时发现一些意外行为。出现问题是因为我使用了 SystemColors 类中使用的名称。

考虑以下代码:

首先,我将 a 中的 ComponentResourceKey 公开为静态属性:


public enum ColorKeys
{
    ControlColor
}

public class MyColors
{
    public static ComponentResourceKey ControlColorKey
    {
        get 
        { 
            return new ComponentResourceKey(typeof(MyColors), ColorKeys.ControlColor); 
        }
    }
}

使用标记中的键将资源分配给该键是可行的。使用 ComponentResourceKey 标记将该资源作为 DynamicResource 调用是可行的。但使用静态属性调用同一资源将解析为 SystemColors.ControlColorKey 资源。

这是 xaml:

<Window.Resources>
    <Color x:Key="{x:Static l:MyColors.ControlColorKey}">#FF0000FF</Color>
    <SolidColorBrush x:Key="UsingVerbose" 
                     Color="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type l:MyColors}, 
                                ResourceId={x:Static l:ColorKeys.ControlColor}}}"/>
    <SolidColorBrush x:Key="UsingStatic" 
                     Color="{DynamicResource {x:Static l:MyColors.ControlColorKey}}"/>                
</Window.Resources>
<Grid>
    <StackPanel>
        <Button Height="20" Width="80" Background="{DynamicResource UsingVerbose}" Content="Verbose"/>
        <Button Height="20" Width="80" Background="{DynamicResource UsingStatic}" Content="Static"/>
    </StackPanel>
</Grid>

显示窗口时,“详细”按钮正确显示蓝色背景。但下方的“静态”按钮显示 SystemColors.Control ColorKey 颜色。

将静态属性的名称更改为另一个名称可以使两个画笔解析为正确的颜色。

但我不确定为什么 DynamicResource 对于密钥的静态实例和由标记实例化的实例的解析方式不同?

Thanks,

K

PS:这是ResourceDictionary Source的相关跟踪输出

System.Windows.ResourceDictionary Start: 10 : New resource dictionary set; Owner='TestMergedDictionaries.Window1'; Owner.HashCode='64923656'; Owner.Type='TestMergedDictionaries.Window1'; OldDictionary='0'; NewDictionary='System.Windows.ResourceDictionary'; NewDictionary.HashCode='44624228'; NewDictionary.Type='System.Windows.ResourceDictionary'
System.Windows.ResourceDictionary Stop: 10 : 
System.Windows.ResourceDictionary Start: 12 : Deferred resource has been added to ResourceDictionary; Dictionary='System.Windows.ResourceDictionary'; Dictionary.HashCode='44624228'; Dictionary.Type='System.Windows.ResourceDictionary'; ResourceKey='TargetType=TestMergedDictionaries.MyColors ID=ControlColor'; ResourceKey.HashCode='1936592'; ResourceKey.Type='System.Windows.ComponentResourceKey'
System.Windows.ResourceDictionary Stop: 12 : 
System.Windows.ResourceDictionary Start: 12 : Deferred resource has been added to ResourceDictionary; Dictionary='System.Windows.ResourceDictionary'; Dictionary.HashCode='44624228'; Dictionary.Type='System.Windows.ResourceDictionary'; ResourceKey='UsingVerbose'
System.Windows.ResourceDictionary Stop: 12 : 
System.Windows.ResourceDictionary Start: 12 : Deferred resource has been added to ResourceDictionary; Dictionary='System.Windows.ResourceDictionary'; Dictionary.HashCode='44624228'; Dictionary.Type='System.Windows.ResourceDictionary'; ResourceKey='UsingStatic'
System.Windows.ResourceDictionary Stop: 12 : 
System.Windows.ResourceDictionary Start: 11 : Searching for resource; Element='System.Windows.Controls.Button'; Element.HashCode='52727599'; Element.Type='System.Windows.Controls.Button'; ResourceKey='UsingVerbose'
System.Windows.ResourceDictionary Start: 3 : Found resource item on an element; Element='TestMergedDictionaries.Window1'; Element.HashCode='64923656'; Element.Type='TestMergedDictionaries.Window1'; ResourceKey='UsingVerbose'; Value='System.Windows.DeferredResourceReference'; Value.HashCode='14347911'; Value.Type='System.Windows.DeferredResourceReference'
System.Windows.ResourceDictionary Stop: 3 : 
System.Windows.ResourceDictionary Stop: 11 : Searching for resource; Element='System.Windows.Controls.Button'; Element.HashCode='52727599'; Element.Type='System.Windows.Controls.Button'; ResourceKey='UsingVerbose'

... removed uninteresting traces ...

System.Windows.ResourceDictionary Start: 11 : Searching for resource; Element='TestMergedDictionaries.Window1'; Element.HashCode='64923656'; Element.Type='TestMergedDictionaries.Window1'; ResourceKey='TargetType=TestMergedDictionaries.MyColors ID=ControlColor'; ResourceKey.HashCode='1936592'; ResourceKey.Type='System.Windows.ComponentResourceKey'
System.Windows.ResourceDictionary Start: 3 : Found resource item on an element; Element='TestMergedDictionaries.Window1'; Element.HashCode='64923656'; Element.Type='TestMergedDictionaries.Window1'; ResourceKey='TargetType=TestMergedDictionaries.MyColors ID=ControlColor'; ResourceKey.HashCode='1936592'; ResourceKey.Type='System.Windows.ComponentResourceKey'; Value='System.Windows.DeferredResourceReference'; Value.HashCode='23488915'; Value.Type='System.Windows.DeferredResourceReference'
System.Windows.ResourceDictionary Stop: 3 : 
System.Windows.ResourceDictionary Stop: 11 : Searching for resource; Element='TestMergedDictionaries.Window1'; Element.HashCode='64923656'; Element.Type='TestMergedDictionaries.Window1'; ResourceKey='TargetType=TestMergedDictionaries.MyColors ID=ControlColor'; ResourceKey.HashCode='1936592'; ResourceKey.Type='System.Windows.ComponentResourceKey'

System.Windows.ResourceDictionary Start: 2 : Delayed creation of resource; Dictionary='System.Windows.ResourceDictionary'; Dictionary.HashCode='23264094'; Dictionary.Type='System.Windows.ResourceDictionary'; ResourceKey='System.Windows.Controls.TextBlock'; Value='DefAttributeKeyType typeId=TextBlock'; Value.HashCode='23521433'; Value.Type='System.Windows.Markup.BamlDefAttributeKeyTypeRecord'
System.Windows.ResourceDictionary Stop: 2 : Delayed creation of resource; Dictionary='System.Windows.ResourceDictionary'; Dictionary.HashCode='23264094'; Dictionary.Type='System.Windows.ResourceDictionary'; ResourceKey='System.Windows.Controls.TextBlock'; Value='System.Windows.Style'; Value.HashCode='5'; Value.Type='System.Windows.Style'

System.Windows.ResourceDictionary Start: 2 : Delayed creation of resource; Dictionary='System.Windows.ResourceDictionary'; Dictionary.HashCode='44624228'; Dictionary.Type='System.Windows.ResourceDictionary'; ResourceKey='UsingStatic'; Value='DefAttributeKeyString'; Value.HashCode='24854661'; Value.Type='System.Windows.Markup.BamlDefAttributeKeyStringRecord'
System.Windows.ResourceDictionary Start: 11 : Searching for resource; Element=''; ResourceKey='ControlColor'; ResourceKey.HashCode='35'; ResourceKey.Type='System.Windows.SystemResourceKey'
System.Windows.ResourceDictionary Start: 8 : Found resource item in theme; Dictionary='System.Windows.SystemResourceHost'; Dictionary.HashCode='65273341'; Dictionary.Type='System.Windows.SystemResourceHost'; ResourceKey='ControlColor'; ResourceKey.HashCode='35'; ResourceKey.Type='System.Windows.SystemResourceKey'; Value='#FFF0F0F0'
System.Windows.ResourceDictionary Stop: 8 : 
System.Windows.ResourceDictionary Stop: 11 : Searching for resource; Element=''; ResourceKey='ControlColor'; ResourceKey.HashCode='35'; ResourceKey.Type='System.Windows.SystemResourceKey'
System.Windows.ResourceDictionary Stop: 2 : Delayed creation of resource; Dictionary='System.Windows.ResourceDictionary'; Dictionary.HashCode='44624228'; Dictionary.Type='System.Windows.ResourceDictionary'; ResourceKey='UsingStatic'; Value='#FFF0F0F0'; Value.HashCode='12408161'; Value.Type='System.Windows.Media.SolidColorBrush'

System.Windows.ResourceDictionary Start: 11 : Searching for resource; Element='TestMergedDictionaries.Window1'; Element.HashCode='64923656'; Element.Type='TestMergedDictionaries.Window1'; ResourceKey='ControlColor'; ResourceKey.HashCode='35'; ResourceKey.Type='System.Windows.SystemResourceKey'
System.Windows.ResourceDictionary Start: 8 : Found resource item in theme; Dictionary='System.Windows.SystemResourceHost'; Dictionary.HashCode='65273341'; Dictionary.Type='System.Windows.SystemResourceHost'; ResourceKey='ControlColor'; ResourceKey.HashCode='35'; ResourceKey.Type='System.Windows.SystemResourceKey'; Value='#FFF0F0F0'
System.Windows.ResourceDictionary Stop: 8 : 
System.Windows.ResourceDictionary Stop: 11 : Searching for resource; Element='TestMergedDictionaries.Window1'; Element.HashCode='64923656'; Element.Type='TestMergedDictionaries.Window1'; ResourceKey='ControlColor'; ResourceKey.HashCode='35'; ResourceKey.Type='System.Windows.SystemResourceKey'

System.Windows.ResourceDictionary Start: 2 : Delayed creation of resource; Dictionary='System.Windows.ResourceDictionary'; Dictionary.HashCode='44624228'; Dictionary.Type='System.Windows.ResourceDictionary'; ResourceKey='TargetType=TestMergedDictionaries.MyColors ID=ControlColor'; ResourceKey.HashCode='1936592'; ResourceKey.Type='System.Windows.ComponentResourceKey'; Value='KeyElementStart typeId=StaticExtension'; Value.HashCode='54371668'; Value.Type='System.Windows.Markup.BamlKeyElementStartRecord'
System.Windows.ResourceDictionary Stop: 2 : Delayed creation of resource; Dictionary='System.Windows.ResourceDictionary'; Dictionary.HashCode='44624228'; Dictionary.Type='System.Windows.ResourceDictionary'; ResourceKey='TargetType=TestMergedDictionaries.MyColors ID=ControlColor'; ResourceKey.HashCode='1936592'; ResourceKey.Type='System.Windows.ComponentResourceKey'; Value='#FF0000FF'


在静态 ControlColorKey 中,每次访问 getter 时都会创建 ComponentResourceKey 的新实例。因此,第二次使用引用了不同的对象,并且没有获得更新的颜色。 如果将代码更改为简单的静态字段,它将按预期工作:

public static ComponentResourceKey ControlColorKey = new ComponentResourceKey(typeof(MyColors), ColorKeys.ControlColor);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ComponentResourceKey 作为 DynamicResource 问题 的相关文章

  • 将事件绑定到 ItemsControl 中的按钮

    我有一个 Windows Phone 7 应用程序 其中包含一些 xaml 如下所示
  • 在资源字典中添加字体系列

    我正在使用msdn教程 http msdn microsoft com en us library ms753303 aspx wa wsignin1 0要在我的 Wpf 应用程序中添加 FontFamily 在 csproj 中我有
  • WPF 还是 WinForms 作为内部工具?

    在我的公司 我们开发了许多内部工具 从简单的实用程序到成熟的编辑器 这些工具的首要任务是稳定性 可用性和周转时间 意味着添加新功能的速度 到目前为止 我们一直在使用 WinForms 但有几个新工具正在酝酿中 我正在权衡是否应该继续使用 W
  • 怎么让画笔变得光滑,中间没有线条

    大家好 正如你在之前的画笔中看到的 中间有线条 不太顺利 如何使其平滑 如何删除该行 我用混合创建它
  • WPF 在菜单上选择项目或在视图模型中获取命令参数

    我正在寻找几个小时来解决一个简单的问题 我想在我的菜单项上使用 SelectedItem 但经过几个小时的 stackoverflow 我发现这是不可能的 我发现了很多关于 CommandParameter 的信息 但我不明白它是如何工作的
  • WPF水平滚动条不可见

    我在 ScrollViewer 中有一个网格 网格只有一列和许多行 我将控件添加到网格行中 问题是 当我添加控件时 我可以看到 VerticalScrollBar 但尽管我添加了非常宽的控件 但我看不到 Horizo ntalScrollB
  • 如何更改 WPF TabControl 的 ItemTemplate 的样式以便设置选项卡标题的背景颜色?

    我使用 WPF TabControl 的 ItemTemplate 属性来绑定每个 TabItem 标头的内容 我还使用 ItemContainerStyle 属性将 TabItem 的内容设置为动态选择的用户控件 此方法 100 按预期工
  • 将快捷键分配给 WPF 中的按钮

    如何为WPF中的按钮分配快捷键 谷歌搜索给了我答案 即在标准 Winforms 中附加 而不是 所以在我完成以下操作后
  • ListBox.ScrollIntoView() 似乎在 WP7 中不起作用

    每当将新项目添加到项目源时 我想将列表框滚动条移动到底部 但是ScrollIntoView 如果我向它传递对新添加项目的引用或其索引 它似乎不会执行任何操作 有没有人让它工作 或者对如何将列表框向下滚动到底部有任何其他建议 一些代码 voi
  • 将 RoutedEvent 转换为 WPF 中 MVVM 模式的命令

    如何将 Checkbox checked 路由事件转换为 MVVM 模式的命令 参考 考虑按钮单击路由事件 当我们为按钮分配命令时 button click 事件被视为命令 它执行相应的命令 我用谷歌搜索了它 我得到了 EventBehav
  • TabItem ContenTemplate 上 IsMouseOver 的触发器

    我对 TabItem 样式有疑问 也许你们中的某个人可以帮助我 遵循有问题的风格
  • WPF 中图像的淡入淡出

    当我更改幻灯片放映等图像源时 如何实现淡入和淡出图像 我的图像从本地和网络加载 并且其数量是可变的 谢谢 您可以编写一个扩展方法 通过动画显示图像来淡出图像Opacity属性设置为 0 然后设置Source属性并最终将不透明度动画回到 1
  • Silverlight:在 XAML 中创建 DataTemplate,并在代码中分配它?

    是否可以在 XAML 中创建 DataTemplate 并通过代码将其应用到各种控件 如果是这样 我如何引用我在 XAML 中构建的 DataTemplate 如果你有
  • 如何从 Access 数据库中读取“是/否”值作为布尔值?

    帮我找回YES NO来自 MS Access 的布尔格式数据类型 我尝试解析它 但它总是返回 false 更新 实际上不是问题抱歉 它确实接受 YES NO 作为布尔值 OleDbconnection dbConnect new OleDb
  • 根据 AD 组成员身份限制对 WPF 视图的访问

    我们有一个 WPF 应用程序 我们希望根据用户的 AD 组成员身份限制对应用程序的访问 我们可以将其作为每个视图的属性 或者作为用户启动应用程序时的检查吗 任何代码示例将不胜感激 在 NET 3 5 及更高版本上执行此操作的最简单方法是使用
  • 全屏模式下的 XBAP

    我想以全屏模式运行 XBAP 有没有办法让IE全屏打开XBAP 由于我以完全信任的方式运行 每当用户单击全屏按钮时 我都会使用 Process Start 方法以 kiosk 模式打开 IE 以下代码解决了我的问题 Process Star
  • 为什么这个 Microsoft.VisualStudio.Imaging.CrispImage 不显示任何内容?

    我正在尝试创建一个非常简单的 WPF 应用程序来实验 Visual Studio 的这个概念图像服务和目录 https msdn microsoft com en us library mt628927 aspx 我不明白为什么我似乎无法在
  • WPF圆形奇怪边框

    Hi 我现在已经尝试了 2 天多的时间来创建处于按下状态的切换按钮 如上图所示 但上边框让我头疼 有谁知道如何创建向下的圆角 背景是从上到下的线性渐变 b8c7d6 a8b3c4 任何帮助将不胜感激 我有这样的东西 但它与设计相去甚远
  • 如何使用 DesignData 帮助开发 Metro 应用程序?

    我一直在 Windows Phone 应用程序中愉快地使用 DesignData 我希望使用它来帮助在 VS2012 Blend for VS 中的 Metro 风格应用程序中可视化设计 我已经尝试过希望显而易见的方法
  • 如何转换WPF项目以便它可以被单独的exe用作类库

    有没有人尝试过或觉得需要在独立的 exe 中启动应用程序并将 WPF 用作另一个 DLL 而不是在其中包含 Main 方法 我看到的优点是逻辑分离 并且能够在 WPF 代码之外设置工厂等 从而减少程序集依赖性 我已经尝试过这样做并且可以做到

随机推荐