Xamarin.forms Xamarin使用修改的可绑定属性默认值形成子类

Xamarin.forms Xamarin使用修改的可绑定属性默认值形成子类,xamarin.forms,Xamarin.forms,我试图得到Xamarin表单“Label”类的一个子类。在我的子类中,在许多其他更改中,我希望对一些可绑定属性(例如FontSize和FontAttributes)使用不同的默认值。但是,如果我在构造函数中设置这些,那么样式说明符似乎不会覆盖这些,可能是因为绑定已经注意到它们使用的是非默认值。有没有办法在子类中指定要在可绑定属性中使用不同的默认值 class MyCustomLabel : Label { public MyCustomLabel() { FontSize=20;

我试图得到Xamarin表单“Label”类的一个子类。在我的子类中,在许多其他更改中,我希望对一些可绑定属性(例如FontSize和FontAttributes)使用不同的默认值。但是,如果我在构造函数中设置这些,那么样式说明符似乎不会覆盖这些,可能是因为绑定已经注意到它们使用的是非默认值。有没有办法在子类中指定要在可绑定属性中使用不同的默认值

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!" />
类MyCustomLabel:标签{
公共MyCustomLabel(){
FontSize=20;
}
}

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

不幸的是,
BindableProperty
似乎不支持
覆盖元数据
,就像
dependencProperty
一样。实现这一点有两种方法

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


我认为这是不可能的。不能重写静态属性(如可绑定属性)。样式在构造函数之前“运行”。如果仅当其为“默认值”时才设置新的默认值,该怎么办?(对不起,我的英语不好)你的英语很好。有没有办法知道默认值是否已更改?一个简单的比较是行不通的,因为有人可能会将其更改回原始版本。恐怕唯一的方法是通过OnPropertyChanged事件。上面#1的一个问题是,我将其放入类库中,因此该库无法访问应用程序样式。我通过创建一个“假”页面来解决这个问题,在那里我添加了资源。然后,我在库中创建了一个“boot”函数,该函数在“app”对象中传递,然后将资源从“fake”页面复制到应用程序的资源中。@johnnyb-顺便说一句,不必创建一个假的页面,只需有一个附加资源的地方,然后将其复制到应用程序资源中。只需创建一个包含

    <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>
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);
}