gpt4 book ai didi

xamarin - 如何创建具有属性的自定义控件

转载 作者:行者123 更新时间:2023-12-01 15:35:21 25 4
gpt4 key购买 nike

我需要创建一个带有属性的自定义控件。

这是我的自定义控件:

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>

更新

我尝试了您的提示,但还有其他问题:

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();
}

最佳答案

无需创建构造函数。以下是如何实现 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); }
}
}

我不知道你想用 View 实现什么。作为开始,我建议您学习 these controlsStacklayout implementation .

关于xamarin - 如何创建具有属性的自定义控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47551744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com