删除资源字典并在 WPF 中添加另一个资源字典
我有两个资源词典。一个名为 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 中将文本块设置为:
<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;
这是正确的方法吗?我们是否必须删除当前词典然后添加词典?
I have two resource dictionaries. One is called ResDictGlass.xaml and the other ResDictNormal.xaml. Both has identical properties and different values. For instance
ResDictGlass.xaml has one style like this:
<Style x:Key="StyleTitleText" TargetType="TextBlock">
<Setter Property="FontFamily" Value="Arial" />
<Setter Property="FontSize" Value="14"/>
<Setter Property="Foreground" Value="Green" />
</Style>
The same style in ResDictNormal.xaml is:
<Style x:Key="StyleTitleText" TargetType="TextBlock">
<Setter Property="FontFamily" Value="Tahoma" />
<Setter Property="FontSize" Value="14"/>
<Setter Property="Foreground" Value="WhiteSmoke" />
</Style>
I set up the textblock in the xaml as:
<TextBlock Style="{DynamicResource StyleTextblock}" Text="Prod.Code" VerticalAlignment="Top" />
I want to switch between these styles at runtime. What I do is like this:
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;
Is this the right approach? Do we have to remove the current dictionary and then add the dictionary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您希望将两个词典之一合并到应用程序中,而不是两者都合并。否则,不明确的资源将在引用时引发错误。
另请记住,如果主题需要动态更新 UI(即无需重新加载整个 UI),您可能需要使用
DynamicResource
而不是StaticResource
。让我知道这是否有帮助。
Yes you would want to have either of the two dictionaries merged in the app and not both. Otherwise ambiguous resourecs will throw error upon their reference.
Also remember that you may need to use
DynamicResource
overStaticResource
if the themes need to update the UI dynamically (i.e. withoput entire UI reloading).Let me know if this helps.