gpt4 book ai didi

wpf - 删除资源字典并在 WPF 中添加另一个资源字典

转载 作者:行者123 更新时间:2023-12-03 00:44:52 25 4
gpt4 key购买 nike

我有两个资源词典。一个名为 ResDictGlass.xaml,另一个名为 ResDictNormal.xaml。两者具有相同的属性和不同的值。例如

ResDictGlass.xaml 有这样一种样式:

<Style x:Key="StyleTitleText" TargetType="TextBlock">
<Setter Property="FontFamily" Value="Arial" />
<Setter Property="FontSize" Value="14"/>
<Setter Property="Foreground" Value="Green" />
</Style>

ResDictNormal.xaml中的相同样式是:

<Style x:Key="StyleTitleText" TargetType="TextBlock">
<Setter Property="FontFamily" Value="Tahoma" />
<Setter Property="FontSize" Value="14"/>
<Setter Property="Foreground" Value="WhiteSmoke" />
</Style>

我在 xaml 中将文本 block 设置为:

 <TextBlock Style="{DynamicResource StyleTextblock}" Text="Prod.Code" VerticalAlignment="Top" />

我想在运行时在这些样式之间切换。我的做法是这样的:

           case "normal":
ResourceDictionary ResDict1 = new ResourceDictionary();
ResDict1.Source = new Uri("/ResDictNormal.xaml", UriKind.RelativeOrAbsolute);
Application.Current.Resources.MergedDictionaries.Add(ResDict1);
break;

case "flip":
ResourceDictionary ResDict2 = new ResourceDictionary();
ResDict2.Source = new Uri("/ResDictGlass.xaml", UriKind.RelativeOrAbsolute);
Application.Current.Resources.MergedDictionaries.Add(ResDict2);
break;

这是正确的方法吗?我们是否必须删除当前词典然后添加词典?

最佳答案

是的,您希望在应用程序中合并两个词典中的一个,而不是两者。否则,不明确的资源将在引用时抛出错误。

另请记住,如果主题需要动态更新 UI(即无需重新加载整个 UI),您可能需要使用 DynamicResource 而不是 StaticResource

请告诉我这是否有帮助。

关于wpf - 删除资源字典并在 WPF 中添加另一个资源字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7400497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com