我需要创建一个具有属性的自定义控件。
这是我的自定义控件:
public class ExpandableStackLayout : StackLayout
{
public String Title{get;set;}
public ContentView View { set; get; }
public String Image{set;get;}
public ExpandableStackLayout(string title, string image, ContentView view)
{
Title = title;
Image = image;
View = view;
}
}
但是,当我尝试在XAML中使用它时,说:"没有默认构造函数",如果我创建默认构造函数,请调用此,而不是使用参数调用构造函数。
这是我的XAML:
<control:ExpandableStackLayout Title="Title" Image="imag.png">
<control:ExpandableStackLayout.View>
<ContentView>
<OtherView/>
</ContentView>
</control:ExpandableStackLayout.View>
</control:ExpandableStackLayout>
Update
我尝试了您的提示,但我有其他问题:
public static readonly BindableProperty TitleProperty = BindableProperty.Create(
propertyName: "Title",
returnType: typeof(String),
declaringType: typeof(String),
defaultValue: String.Empty);
public String Title
{
get => (String)GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}
public ContentView View { set; get; }
public static readonly BindableProperty ImageProperty = BindableProperty.Create(
propertyName: "Image",
returnType: typeof(String),
declaringType: typeof(String),
defaultValue: string.Empty);
public String Image
{
get => (String)GetValue(TitleProperty);
set => SetValue(ImageProperty, value);
}
public ExpandableStackLayout()
{
//Do somethings whith parameters but are null
usemyParameters();
}
无需创建构造函数。这是如何实现标题和图像属性的解决方案。
public class ExpandableStackLayout : StackLayout
{
public static readonly BindableProperty TitleProperty =
BindableProperty.Create<ExpandableStackLayout, string>(p => p.Title, string.Empty, BindingMode.TwoWay);
public string Title
{
get { return (string)base.GetValue(TitleProperty); }
set { base.SetValue(TitleProperty, value); }
}
public static readonly BindableProperty ImageProperty =
BindableProperty.Create<ExpandableStackLayout, string>(p => p.Image, string.Empty, BindingMode.TwoWay);
public string Image
{
get { return (string)base.GetValue(ImageProperty); }
set { base.SetValue(ImageProperty, value); }
}
}
我不知道您想实现什么看法。首先,我建议您研究这些控件和堆叠实现。
用于从.xAML到.cs代码的绑定变量,您应该使用可绑定的属性。
您的.cs文件应该看起来像
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ExpandableStackLayout : StackLayout
{
public ExpandableStackLayout ()
{
this.InitializeComponent();
}
public static readonly BindableProperty TitleProperty = BindableProperty.Create(nameof(Title), typeof(string), typeof(ExpandableStackLayout), string.Empty);
public string Title
{
get
{
return (string)GetValue(TitleProperty);
}
set
{
SetValue(TitleProperty, value);
}
}
}
自定义视图的属性不应通过构造函数设置。实现自定义视图属性的方法是可约束性属性。
基本上,您必须创建两件事才能创建一个完全露出的可约束性属性:
-
public static BindableProperty
- a c#属性获得并在显式getter和setter中分别设置
BindableProperty
我将以Title
属性为例进行解释。首先创建一个简单的自动属性(C#属性和BindableProperty
是相互依存的)
public string Title { get; set; }
在第二步中,您创建BindableProperty
public static readonly BindableProperty TitleProperty
= BindableProperty.Create(nameof(Title), typeof(string), typeof(ExpandableStackLayout));
TitleProperty
是一个命名约定,可从您的XAML访问Title
。
最后一步是通过Title
TitleProperty
的值 public string Title
{
get => (string)GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}
这简化了访问TitleProperty
。