所有其他视图的Xamarin基本视图

时间:2017-12-15 17:05:57

标签: c# xamarin

我想创建一个派生自Xamarin.Forms.View类的基类,该基类具有从该基类派生的所有其他类的公共属性。

这是我创建的示例:

public class VolosBaseView : View {

    public bool Required {
        get { return (bool)GetValue(RequiredProperty); }
        set { SetValue(RequiredProperty, value); }
    }

    private static readonly BindableProperty RequiredProperty = BindableProperty.Create(nameof(Required), typeof(bool), typeof(VolosTextEntryView));

    protected override void OnPropertyChanged(string propertyName = null) {
        base.OnPropertyChanged(propertyName);

        if (propertyName == RequiredProperty.PropertyName) {
            if (Required) {
                BackgroundColor = Color.LightYellow;
            } else {
                BackgroundColor = Color.Default;
            }
        }
    }
}

现在我需要为每个Xamarin.Forms视图类(EntryPickerSwitch等等)创建一个类,这些类派生自此基类,但如果我创建一个这样的类(这是Entry):

public class VolosTextEntryView : VolosBaseView {

    public VolosTextEntryView() {
    }
}

我的视图丢失了Entry基类的所有属性。

如何创建具有公共属性的基类,并创建从其中派生的所有其他类以及其他Xamarin.Forms视图类之一的条目?

谢谢。

1 个答案:

答案 0 :(得分:2)

以你所说的方式实现它是不可能的。 C#不能有多重继承。

您可以使用the pattern suggested here

进行模拟

尽管如此,您可以解决创建界面的问题,例如,您的View MyViewEntry应该继承Entry并实现IBaseView。