我想创建一个派生自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
视图类(Entry
,Picker
,Switch
等等)创建一个类,这些类派生自此基类,但如果我创建一个这样的类(这是Entry
):
public class VolosTextEntryView : VolosBaseView {
public VolosTextEntryView() {
}
}
我的视图丢失了Entry
基类的所有属性。
如何创建具有公共属性的基类,并创建从其中派生的所有其他类以及其他Xamarin.Forms
视图类之一的条目?
谢谢。
答案 0 :(得分:2)
以你所说的方式实现它是不可能的。 C#不能有多重继承。
您可以使用the pattern suggested here
进行模拟尽管如此,您可以解决创建界面的问题,例如,您的View MyViewEntry
应该继承Entry
并实现IBaseView。