【发布时间】:2017-02-06 04:54:35
【问题描述】:
我想在 xamarin.forms 中创建用户控件(用户视图)。我的控件有一个属性。控制选择要添加到页面的元素(条目或标签)。为此,我使用 BindableProperty,但它只返回默认值。不明白哪里错了?
这里是我的用户控制代码:
public partial class UserView : ContentView
{
public UserView()
{
InitializeComponent();
StackLayout stackLayout = new StackLayout();
if (TypeElement == "label") //TypeElement return only "default value"
stackLayout.Children.Add(new Label { Text = "LABEL" });
else
stackLayout.Children.Add(new Entry { Text = "ENTRY" });
Content = stackLayout;
}
public static readonly BindableProperty TypeProperty = BindableProperty.CreateAttached("TypeElement", typeof(string), typeof(UserView), "default value");
public string TypeElement
{
get
{
return (string)GetValue(TypeProperty);
}
set
{
SetValue(TypeProperty, value);
}
}
}
这里我在页面上使用我的控件:
【问题讨论】:
标签: c# xamarin.forms