具有修改的可绑定属性默认值的Xamarin Forms子类 [英] Xamarin Forms Subclass with Modified Bindable Property Defaults

查看:60
本文介绍了具有修改的可绑定属性默认值的Xamarin Forms子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取Xamarin Forms标签"类的子类.在我的子类中,除其他许多更改外,我想为某些可绑定属性(例如FontSize和FontAttributes)设置一个不同默认值.但是,如果我在构造函数中设置它们,则似乎Style指定符不会覆盖它们,大概是因为绑定已经注意到它们正在使用非默认值.有没有一种方法可以在子类中指定要在可绑定属性中使用不同的默认值?

I am trying to get a subclass of the Xamarin Forms "Label" class. In my subclass, among a lot of other changes, I want to have a different default value for some bindable properties (such as FontSize and FontAttributes). However, if I set these in the constructor, it seems that Style specifiers won't override these, presumably because the bindings are already noticing that they are using non-default values. Is there a way to specify in a subclass that you want to use different default values in a bindable property?

class MyCustomLabel : Label {
  public MyCustomLabel() {
    FontSize=20;
  }
}

<ResourceDictionary>
  <Style x:Key="Superbig" TargetType="MyCustomLabel">
    <Setter Property="FontSize" Value="3" />
  </Style>
</ResourceDictionary>

<MyCustomLabel Style="{StaticResource Superbig}" Text="Hi There!" />

在这里,由于我在构造函数中设置了新的默认值,因此未应用Superbig样式.因此,我希望(a)可以使用其他方法设置新的默认值,或者(b)可以使用其他方法设置样式,以使其覆盖已经设置的任何值.

Here, the Superbig style is not being applied because I am setting the new default value in the constructor. Therefore, I was hoping either (a) there was some other way to set a new default value, or (b) there was some other way to set a style so it overrode any value that was already set.

推荐答案

不幸的是,BindableProperty似乎不像DependencyProperty那样支持OverrideMetadata.这是实现此目的的两种方法.

Unfortunately, BindableProperty doesn't seem to support OverrideMetadata like DependencyProperty does. Here's two way to achieve this.

1)为MyCustomLabel对象(XAML)设置默认的Style

1) Set a default Style for your MyCustomLabel object (XAML)

    <ResourceDictionary>
        <!--Default style-->
        <Style TargetType="local:MyCustomLabel">
            <Setter Property="FontSize" Value="10" />
        </Style>

        <!--Superbig style-->
        <Style x:Key="Superbig" TargetType="local:MyCustomLabel">
            <Setter Property="FontSize" Value="40" />
        </Style>
    </ResourceDictionary>

2)创建一个新的FontSize BindableProperty(C#)

2) Create a new FontSize BindableProperty (C#)

public class MyCustomLabel : Label
{
    public MyCustomLabel()
    {
        base.SetBinding(Label.FontSizeProperty, new Binding(nameof(FontSize))
        {
            Source = this,
            Mode = BindingMode.OneWay
        });
    }

    //Don't forget the "new" keyword
    public new double FontSize
    {
        get { return (double)GetValue(FontSizeProperty); }
        set { SetValue(FontSizeProperty, value); }
    }

    //Don't forget the "new" keyword
    public static readonly new BindableProperty FontSizeProperty =
        BindableProperty.Create(nameof(FontSize), typeof(double), typeof(MyCustomLabel), 40.0);
}

这篇关于具有修改的可绑定属性默认值的Xamarin Forms子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆