c# - WPF中的静态资源和动态资源有什么区别?

标签 c# wpf resources wpf-controls

WPF 中的 StaticResources 和 DynamicResources 有什么区别?

编辑: XAML 文件中的这段代码:

<ComboBox Canvas.Left="14" Style="{StaticResource ComboBoxStyle}"
          Canvas.Top="137" Height="33" Name="cmbItem" Width="170"
          SelectionChanged="cmbItem_SelectionChanged">
    <ComboBoxItem>Name</ComboBoxItem>
    <ComboBoxItem>Age</ComboBoxItem>
</ComboBox>

以下代码在资源字典文件中

<Style x:Key="ComboBoxStyle" TargetType="{x:Type ComboBox}">

    <Setter Property="FocusVisualStyle" Value="{StaticResource ComboBoxFocusVisual}"/>

    <Setter Property="Foreground" Value="#FF436B13"/>
</Style>

最佳答案

动态资源在您使用时进行评估。静态资源在加载时进行评估。

来自 MSDN :

When you use a markup extension, you typically provide one or more parameters in string form that are processed by that particular markup extension, rather than being evaluated in the context of the property being set. The StaticResource Markup Extension processes a key by looking up the value for that key in all available resource dictionaries. This happens during loading, which is the point in time when the loading process needs to assign the property value that takes the static resource reference. The DynamicResource Markup Extension instead processes a key by creating an expression, and that expression remains unevaluated until the application is actually run, at which time the expression is evaluated and provides a value.

关于何时应该选择哪个选项,那里有更多详细信息。

关于c# - WPF中的静态资源和动态资源有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1420471/

相关文章:

c# - 当 child 发生变化时更新 parent

wpf - 路径的笔画厚度不变,与比例无关

java - 如何从 Vaadin/Spring 应用程序提供静态资源?

javascript - 如何在 javascript 文件中使用 ASP.NET Resource(.resx) 字符串

c# - Winforms 中的菜单类似于文件夹资源管理器菜单

c# - 使用 dotnetopenauth 的 Facebook/Twitter?

c# - 使用存储过程 C# 的结果填充列表框

c# - 如何以编程方式在企业 Wiki 站点的页面库中创建可编辑的 Wiki 页面

wpf - 是否可以强制窗口的大小永远不会低于其子项所需的大小?

java - 调度程序 servlet 和 mvc :resources conflict error: No matching constant for [0]