DynamicResource 不起作用,但 StaticResource 起作用?

发布于 2024-08-14 09:23:44 字数 661 浏览 2 评论 0原文

场景如下:

1) 打开 Visual Studio 2008,创建新项目...

2) 选择 WPF 自定义控件库作为项目类型

3) 打开位于 Themes 文件夹中的 Generic.xaml 资源字典

4) 添加一个简单的画笔,例如如:

< SolidColorBrush x:Key="BackgroundBrush" Color="Yellow" / >

5) 将嵌套 Borer Control 的背景属性从

Background="{TemplateBinding Background}"

更改为

>Background="{DynamicResource BackgroundBrush}"

现在,当您在任何项目中编译并使用此控件时,背景将不再是黄色。但是,如果您将背景属性更改为 StaticResource,例如

Background="{StaticResource BackgroundBrush}"

...它将起作用。这是为什么呢?为什么 StaticResource 可以工作,而 DynamicResource 不行???

Here is the scenario:

1) Open Visual Studio 2008, create new project...

2) Select WPF Custom Control Library as the project type

3) Open the Generic.xaml resource dictionary located in the Themes folder

4) Add a simple brush such as:

< SolidColorBrush x:Key="BackgroundBrush" Color="Yellow" / >

5) Change the Background property of the nested Borer Control from

Background="{TemplateBinding Background}"

to

Background="{DynamicResource BackgroundBrush}"

Now when you compile and use this control in any project, the background will NOT be yellow. BUT, if you change the background property to be a StaticResource as such

Background="{StaticResource BackgroundBrush}"

...it will work. Why is this? Why does StaticResource work but not DynamicResource???

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

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

发布评论

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

评论(1

天涯沦落人 2024-08-21 09:23:45

如果您在 Xaml 文件中对画笔进行硬编码,那么对我来说这听起来相当静态。

DynamicResource 的 MSDN 条目说这是当您想要使用时使用的在运行时解析资源的名称(即该名称存储在另一个资源中),您在编译时就知道该名称。

听起来很像 C 中的指针和指向指针的指针。

If you are hard coding the brush in Xaml file, that sounds pretty static to me.

The MSDN entry for DynamicResource say this is for when you want the use resolve the name of the resource at run time (ie the name is stored in a another resource), where you know the name at compile time.

Sounds very much like a pointer verse a pointer-to-a-pointer from C.

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