【问题标题】:How to easily allow users to update Styles used be elements in XAML (UWP)如何轻松地让用户更新 XAML (UWP) 中使用的元素样式
【发布时间】:2018-08-16 02:35:00
【问题描述】:

这适用于 Windows 10 UWP。我需要允许用户更新与整个应用程序中使用的元素相关联的样式值(即允许用户更改各种文本块的字体大小、背景颜色堆栈面板等)。

我目前将所有样式保存在一个单独的文件中。

我的App.xaml如下:

<Application
    x:Class="MyTestApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Application.Resources>

        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Styles/Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

    </Application.Resources>

</Application>

我的Styles.xaml(部分)如下:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:converters="using:MyTestApp.Views"
    xmlns:x1="using:System">

    <Style x:Key="HeaderTextBlocks" TargetType="TextBlock" BasedOn="{StaticResource TitleTextBlockStyle}">
        <Setter Property="FontSize" Value="24"/>
        <Setter Property="FontWeight" Value="Normal"/>
        <Setter Property="TextWrapping" Value="NoWrap"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="VerticalAlignment" Value="Bottom"/>
        <Setter Property="Margin" Value="10,4,0,0"/>
    </Style>

    <Style x:Key="RegularTextBlocks" TargetType="TextBlock" BasedOn="{StaticResource TitleTextBlockStyle}">
        <Setter Property="FontSize" Value="12"/>
        <Setter Property="FontWeight" Value="Normal"/>
        <Setter Property="TextWrapping" Value="NoWrap"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="VerticalAlignment" Value="Bottom"/>
        <Setter Property="Margin" Value="0,0,0,0"/>
    </Style>
</ResourceDictionary>

我在整个应用程序中使用这样的方式在控件上引用这些样式:

<TextBlock Style="{StaticResource HeaderTextBlocks}" />

我创建了一个设置页面 (settings.xaml),其中包含供用户更新各种样式设置的文本框。

但我不确定如何将这些绑定到styles.xaml 文件中各种样式的设置,以便在用户更改值时更新样式并更新引用样式的控件.

<TextBox Header="Font Size of Header TextBlocks" Text="{x:Bind HeaderTextBlocks.FontSize ???, Mode=TwoWay}" />
<TextBox Header="Font Size of Regular TextBlocks" Text="{x:Bind RegularTextBlocks.FontSize???, Mode=TwoWay}" />

有人可以指出我正确的方向吗?我试图用尽可能少的(或没有代码)来做到这一点。

【问题讨论】:

  • @TobiasTheel 感谢您的回复!但我看不到如何将 DynamicResource 与 Windows 10 UWP 一起使用:/
  • 很遗憾,DynamicResource 标记扩展在 UWP 中不可用

标签: c# xaml uwp windows-10-universal


【解决方案1】:

不幸的是,这种用户定义的样式在 UWP 中并不容易使用。但是,您可以使用数据绑定实现一种样式解决方案。

第一步是创建一个类似CustomUISettings 的类,它实现INotifyPropertyChanged 并具有HeaderFontSize 等属性。

现在在应用启动时创建一个此类的实例并将其添加为应用资源:

Application.Current.Resources["CustomUISettings"] = new CustomUISettings();

现在您可以在代码中的任何位置绑定到此类中的属性:

<TextBox FontSize="{Binding HeaderFontSize, Source={StaticResource CustomUISettings}}" />

您必须使用经典的{Binding} 标记扩展,因为{x:Bind} 不支持Source 设置。

要修改 UI 设置,您可以在任何地方检索实例并根据需要设置属性:

var customUISettings = (CustomUISettings)Application.Current.Resources["CustomUISettings"];
customUISettings.HeaderFontSize = 50;

您必须确保CustomUISettings 类中的所有属性都会触发PropertyChanged 事件。您可以查看如何实现INotifyPropertyChanged 接口,例如here

【讨论】:

  • Fwiw,x:Bind 可以绑定到静态,所以如果你想也可以将 CustomUISettings 设为设置类。
  • 真的,我忘了 :-) 。我会用这个更新我的答案!谢谢你的提醒!
  • @Johnny Westlake - 但是等等,我很困惑,Binding 可以绑定到静态,但是 x:Bind 可以吗? x:Bind 仅绑定到代码隐藏属性和字段,或者是否有其他语法允许这样做?而且 - 静态类的问题是它无法实现INotifyPropertyChanged,这是一个问题......
  • 如果我有一个静态类“MyApp.Converters”,你可以添加一个命名空间定义(例如 xmlns:my="using:MyApp")然后你可以使用 {x:Bind my: Converters.Invert(...)},它也适用于数据模板——在这种情况下,您可以使用 {x:Bind my:Resources.CustomUISettings.FontSize, Mode=OneWay},其中 CustomUISettings 仍然是静态实例中的实例跨度>
  • @Johnny Westlake:这很奇怪,我无法让它最终工作 - 我总是得到“无效的绑定路径 - 在类型上找不到属性“my:Converters”主页:-O
猜你喜欢
  • 1970-01-01
  • 2019-02-07
  • 2020-09-23
  • 2019-09-06
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 2018-06-24
  • 2016-09-04
相关资源
最近更新 更多