是否可以从 ResourceDictionary 绑定 FontSize 的值?

时间:2021-02-10 17:51:12

标签: c# xaml xamarin.forms

我正在开发一个应用程序,我需要在设置页面的帮助下将文本大小从小到大更改。

我已经将我的代码从 app.xaml 中分离出来,并从我创建的一些 ResourceDictionary 中添加了一些引用。

我现在想知道是否可以绑定该值。

    <Style x:Key="articleBodyText" TargetType="Label">
        <Setter Property="FontSize" Value="{Binding SelectedFontSize}"/>
        <Setter Property="Padding" Value="0,0,0,15"/>
        <Setter Property="TextColor" Value="{AppThemeBinding Dark={StaticResource LightPrimaryColor}, Light={StaticResource DarkPrimaryColor}}"></Setter>
    </Style>

2 个答案:

答案 0 :(得分:1)

定义一个double类型的属性

    private double _SelectedFontSize;
    public double SelectedFontSize
        {
            get => _SelectedFontSize;
            set => SetProperty(ref _SelectedFontSize, value);  //INotifyProertyChanged
        }

假设您已经在 xaml 或代码中定义了 CustomSmall 资源:

    Resources.Add("CustomSmall", 10);
    Resources.Add("CustomLarge", 22);
    Resources.TryGetValue("CustomSmall", out var fontSize);
    FontSize = (double)fontSize;

相关问题

How can I add application resources to my C# backend for the application instead of in the XAML file?

答案 1 :(得分:0)

例如,Value=new Binding(16) 应将 Fontsize 设置为 16。因此,您可以从设置页面传递所选值并在绑定中使用它。

相关问题