我想为 XAML 实现一种工厂模式。我为 WinRT 创建了一个应用程序,我在其中定义了两个 xaml 样式文件。基本上,我想要实现的(如果可能的话)是在应用程序启动时加载两个 xaml 文件之一。 在解决方案资源管理器中,我有这个:
CustomStyles 文件夹 包含样式文件。因此,基于我的 App.xaml.cs 文件中的枚举器
public enum Style
{
Style_1,
Style_2
}
如果我选择 Style_1,我想在运行时加载 xaml 文件 Style_1.xaml 否则 Style_2.xaml。 这两个样式文件具有相同的 Button 样式、TextBlock 样式等定义,但属性值不同。 这里有一个例子:
Style_1.xaml
<Style x:Key="Attribute_Label" TargetType="TextBlock">
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="Foreground" Value="#78CAB3" />
<Setter Property="FontSize" Value="15" />
<Setter Property="FontWeight" Value="Normal" />
</Style>
Style_2.xaml
<Style x:Key="Attribute_Label" TargetType="TextBlock">
<Setter Property="FontFamily" Value="Arial" />
<Setter Property="Foreground" Value="#606060" />
<Setter Property="FontSize" Value="30" />
<Setter Property="FontWeight" Value="Normal" />
</Style>
有办法实现我想做的事吗?提前谢谢你。
最佳答案
我们最终会做这样的事情:
在 App.xaml 中使用所有 CustomStyles 定义一个 ResourcesDictionary
我们发出一个服务器请求,决定必须加载哪个自定义样式
使用这段代码
Application.Current.Resources[CustomStyleVariable];
我们将整个样式加载到 Style 对象中。
我们还没有找到任何更好的解决方案,但它似乎可行。
关于c# - XAML WinRT - 自定义样式的工厂模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20663094/