我需要创建一个带有属性的自定义控件。
这是我的自定义控件:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试在xaml中使用它时,它说:“找不到默认构造函数”,如果我创建了默认构造函数,则调用此函数,而不是使用参数调用构造函数。
这是我的xaml:
<control:ExpandableStackLayout Title="Title" Image="imag.png">
<control:ExpandableStackLayout.View>
<ContentView>
<OtherView/>
</ContentView>
</control:ExpandableStackLayout.View>
</control:ExpandableStackLayout>
Run Code Online (Sandbox Code Playgroud)
更新
我尝试了您的提示,但还有其他问题:
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);
} …Run Code Online (Sandbox Code Playgroud)