XAML WinRT - 自定义样式的工厂模式

时间:2013-12-18 16:14:46

标签: c# xaml winrt-xaml

我想为XAML实现一种工厂模式。我为WinRT创建了一个应用程序,我在其中定义了两个xaml样式文件。基本上,我想要实现的(如果可能的话)是在应用程序启动时加载两个xaml文件中的一个。 在解决方案资源管理器中我有这个:

enter image description here

CustomStyles foder 包含样式文件。因此,基于 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>

有办法实现我想做的事吗?提前谢谢。

1 个答案:

答案 0 :(得分:2)

我们最终会做这样的事情:

  1. 在App.xaml中使用所有CustomStyles定义 ResourcesDictionary

  2. 我们发出服务器请求,决定必须加载哪种自定义样式

  3. 使用这段代码Application.Current.Resources[CustomStyleVariable];,我们在Style对象中加载整个样式。

  4. 我们还没有找到更好的解决方案,但似乎有效。

相关问题