Mig*_*ñoz 2 xamarin xamarin.forms
我需要创建一个带有属性的自定义控件。
这是我的自定义控件:
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);
}
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();
}
Run Code Online (Sandbox Code Playgroud)
无需创建构造函数。这是解决方案,如何实现Title和Image属性。
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); }
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道您想使用View实现什么。首先,我建议您研究这些控件和Stacklayout实现。
| 归档时间: |
|
| 查看次数: |
558 次 |
| 最近记录: |