.net WPF 动态资源示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5645586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:28:18  来源:igfitidea点击:

WPF Dynamic resource example

.netwpfresourcesdynamicresource

提问by Ankit

Is there any example which can clearly state the difference between Static and dynamic resource. I know the basic difference that Static is loaded once and gets binded at start while dynamic is loaded at run time and rebinded every time the control reloads.

是否有任何示例可以清楚地说明静态和动态资源之间的区别。我知道静态加载一次并在开始时绑定,而动态在运行时加载并在每次控件重新加载时重新绑定的基本区别。

Thanks in advance

提前致谢

回答by pchajer

If the Desktop Color is changed while the element's application is running, the element keeps its original color:

如果在元素的应用程序运行时更改桌面颜色,元素将保持其原始颜色:

<Button>
  <Button.Background>
    <SolidColorBrush Color="{StaticResource {x:Static SystemColors.DesktopColorKey}}" />
  </Button.Background>
  Hello
</Button>

On the other hand, if the element's color is set using a DynamicResource, it changes when the Desktop Color changes:

另一方面,如果元素的颜色是使用 DynamicResource 设置的,它会在桌面颜色更改时更改:

 <Button>
      <Button.Background>
        <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.DesktopColorKey}}" />
      </Button.Background>
      Hello
    </Button>