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

标签 c# xaml winrt-xaml

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

enter image description here

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>

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

最佳答案

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

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

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

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

我们还没有找到任何更好的解决方案,但它似乎可行。

关于c# - XAML WinRT - 自定义样式的工厂模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20663094/

相关文章:

c# - 在 XAML 中找不到程序集引用,但在 xaml.cs 类中引用时代码会编译

WPF:如何从 DLL 资源文件设置图像源?

winrt-xaml - ScrollViewer.ChangeView 返回 false

mvvm - 根据不同的数据绑定(bind)到自定义构建的控件

c# - HttpClient 中的 GetAsync 无法按预期工作

javascript - 用于 React Native/JS 的 SHA256 ComputeHash(来自 C#)的等效版本

c# - 如何删除某些列的标题下划线?

c# - WPF C# 中的日历控件事件问题

c# - Windows 10 中的 Monogame 鼠标仅在移动后感觉有压力

c# - Metro App 全局变量 C#