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/