WPF StaticResource 有效,DynamicResource 无效 [英] WPF StaticResource works, DynamicResource doesn't

查看:46
本文介绍了WPF StaticResource 有效,DynamicResource 无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了一天,但无济于事,在主题中创建一堆画笔,然后在自定义控件中将它们与 DynamicResource 一起使用.我所做的是这样的:

I have been trying for a day now, to no avail, to create a bunch of brushes in the theme then using them with DynamicResource in a custom control. What I did is this:

  • 创建包含样式(作品)的主题 generic.xaml
  • 在 generic.xaml 中添加要合并的字典,以包含应用程序中使用的画笔(作品)
  • 使画笔具有 ComponentResourceKey 键(有效)
  • 使控件使用画笔作为静态资源(作品)
  • 使控件使用画笔作为动态资源(不起作用,资源跟踪源说明了同样多:System.Windows.ResourceDictionary 警告:9:找不到资源;)
  • 在 App.Resources 中动态添加具有相同键的画笔(适用于动态资源,它会更改颜色,不适用于静态资源,正如预期的那样)

所以我的问题是我找不到任何方法来定义主题中的默认值,以便我可以在应用程序中以编程方式更改它们.StaticResource怎么能找到画笔,而DynamicResource不能呢?!

So my problem is that I can't find any way to define the default values in the theme so that I can change them programatically in the application. How can StaticResource find the brush and DynamicResource not?!

我必须补充一点,我已经创建了一个静态类,其中包含组件资源键作为属性,然后我在 xaml 中将其用作 {x:Static UI:ResourceScheme.ControlBackgroundKey} 例如.我的问题似乎与此类似:ComponentResourceKey 作为 DynamicResource 问题 仅当我替换静态组件资源键的 XAML 标记的属性键,它仍然不起作用.

I must add that I've created a static class holding the component resource keys as properties that I then use in the xaml as {x:Static UI:ResourceScheme.ControlBackgroundKey} for example. My problem seems similar to this one: ComponentResourceKey as DynamicResource problem only that if I replace the static property keys to the XAML markup for component resource key, it still doesn't work.

有人可以帮我吗?:(

推荐答案

终于修复了.似乎在另一个程序集中具有组件资源键的类型导致了整个问题.让我总结一下:

Finally fixed it. It appears that having the type for the component resource key in another assembly caused the whole issue. Let me summarize:

  • 有一个资源类将 ComponentResourceKeys 保存为静态属性.资源键的构造函数中使用的类型就是这个类的类型.这是在资源程序集中.
  • 另一个程序集中的自定义控件有一个主题,即控件程序集,它定义了一些使用资源类属性作为键的画笔:{x:Static Namespace:ResourceClass.ResourceKeyProperty}
  • 在同一主题中,控件模板使用画笔作为动态资源:{DynamicResource {x:Static Namespace:ResourceClass.ResourceKeyProperty}}
  • 还有一个应用程序使用这些控件并在应用程序资源中动态添加自定义画笔.这些画笔与主题中的画笔具有相同的键.

最终结果是:

  • 控件最初不使用画笔
  • 控件确实使用了应用程序资源中添加的画笔
  • 如果在主题中使用了 StaticResource,则控件最初会使用画笔,但随后会忽略应用程序资源

对此的解决方案似乎是移动控件库中的资源类.

The solution for this seems to be to move the resources class in the controls library.

由于我仍然不知道为什么会发生这种情况,这个问题仍然存在,即使略有变化:为什么它在第一种情况下不起作用?

As I still do not know why this is happening, this question remains open, even if slightly changed: why doesn't it work in the first scenario?

这篇关于WPF StaticResource 有效,DynamicResource 无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆