Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何轻松允许用户更新XAML(UWP)中元素使用的样式_C#_Xaml_Uwp_Windows 10 Universal - Fatal编程技术网

C# 如何轻松允许用户更新XAML(UWP)中元素使用的样式

C# 如何轻松允许用户更新XAML(UWP)中元素使用的样式,c#,xaml,uwp,windows-10-universal,C#,Xaml,Uwp,Windows 10 Universal,这是针对Windows10UWP的。我需要允许用户更新与整个应用程序中使用的元素相关联的样式值(即允许用户更改各种文本块、背景色面板等的字体大小) 目前,我的所有样式都在一个单独的文件中 我的App.xaml如下: <Application x:Class="MyTestApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.

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

目前,我的所有样式都在一个单独的文件中

我的
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>
<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}" />


有人能给我指一下正确的方向吗?我正在尽可能少地(或没有代码隐藏)完成这项工作。

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

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

现在在app start上创建此类的实例并将其添加为app resource:

Application.Current.Resources["CustomUISettings"] = new CustomUISettings();
现在,您可以在代码中的任意位置绑定到此类中的属性:

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

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

@tobiasheel感谢您的回复!但我不知道如何将DynamicSource与Windows 10 UWP一起使用:/不幸的是,
DynamicSource
标记扩展在UWPWIW中不可用,x:Bind可以绑定到静态,所以如果您愿意,也可以将CustomUISettings设置为一个设置类。没错,我忘记了:-)。我会用这个更新我的答案!谢谢你的提醒@约翰尼·韦斯特莱克-但是等等,我很困惑,绑定可以绑定到静态,但是x:bind吗?x:Bind只绑定到代码隐藏属性和字段,或者是否有其他语法允许这样做?此外,静态类的问题是它无法实现
INotifyPropertyChanged
,这在这里是个问题…如果我有一个静态类“MyApp.Converters”,你可以添加一个名称空间def(例如xmlns:my=“using:MyApp”),然后你可以使用{x:Bind my:Converters.Invert(…)},这也适用于DataTemplates—在本例中,您可以使用{x:Bind my:Resources.CustomUISettings.FontSize,Mode=OneWay},其中CustomUISettings仍然是static@Johnny韦斯特莱克:这很奇怪,我不能让它在我这端工作-我总是得到“无效绑定路径-属性”my:converter“在类型主页面上找不到:-O
var customUISettings = (CustomUISettings)Application.Current.Resources["CustomUISettings"];
customUISettings.HeaderFontSize = 50;