在我的Xamarin.Forms应用程序中,我有一个自定义视图:
[Xamarin.Forms.ContentProperty("Content")]
public class Checkbox : ContentView
{
Label lbl = new Label() { Text = "\u2610" }; // \u2610 Uni code will show empty box
Label lbl1 = new Label() { Text = "Has arrived" };
public string BackgroundColor { get; set; }
public Checkbox()
{
TapGestureRecognizer t = new TapGestureRecognizer();
t.Tapped += OnTapped;
StackLayout stackLayout = new StackLayout()
{
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.StartAndExpand,
Children = {lbl,lbl1}
};
stackLayout.GestureRecognizers.Add(t);
Content = stackLayout;
}
public void OnTapped(object sender, EventArgs args)
{
lbl.Text = lbl.Text == "\u2611" ? "\u2610" : "\u2611"; // \u2611 Uni code will show checked Box
}
}
Run Code Online (Sandbox Code Playgroud)
我在XAML中这样使用它:
<StackLayout Orientation="Horizontal">
<Label Text="My custom view:"/>
<views:Checkbox />
</StackLayout>
Run Code Online (Sandbox Code Playgroud)
如何views:Checkbox从xaml 传递参数?有没有办法绑定我的BackgroundColor财产?
您必须创建一个BindableProperty,因此您的BackgroundColor属性应如下所示:
public string BackgroundColor
{
get { return (string)GetValue(BackgroundColorProperty); }
set { SetValue(BackgroundColorProperty, value); }
}
public static readonly BindableProperty BackgroundColorProperty =
BindableProperty.Create(nameof(BackgroundColor), typeof(string), typeof(Checkbox));
Run Code Online (Sandbox Code Playgroud)
比您可以绑定到它:
<StackLayout Orientation="Horizontal">
<Label Text="My custom view:"/>
<views:Checkbox BackgroundColor="{Binding CheckBoxBgColor}" />
</StackLayout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
859 次 |
| 最近记录: |