[英]How to use a CheckBox in Xamarin.forms?
I'm new in Xamarin.forms and I'm trying to use a multiple CheckBox that I create from a list. 我是Xamarin.forms的新手,并且尝试使用从列表创建的多个CheckBox。
I understand that CheckBox Doesn't exist in Xamarin.forms so I created a class that I finded on Internet to create the control. 我知道Xamarin.forms中不存在CheckBox,所以我创建了一个在Internet上找到的用于创建控件的类。
When I try to create the CheckBox I can't see it. 当我尝试创建CheckBox时,看不到它。 This is the code when I create the CheckBox:
这是我创建CheckBox时的代码:
if (List1 != null && List1.Count > 0)
{
foreach (var c in List1)
{
CheckBox chk = new CheckBox();
chk.CheckedChanged += Chk_CheckedChanged;
chk.IsVisible = true;
chk.CheckBoxBackgroundColor = Color.Blue;
chk.TickColor = Color.Blue;
chk.WidthRequest = 12;
chk.HeightRequest = 12;
StackLayoutBody.Children.Add(chk);
}
}
And this is the code of the CheckBox.cs: 这是CheckBox.cs的代码:
using System;
using Xamarin.Forms;
namespace TECAndroid.Services
{
public class CheckBox : View
{
public static readonly BindableProperty CheckedProperty =
BindableProperty.Create(nameof(Checked), typeof(bool), typeof(CheckBox), false, BindingMode.TwoWay,
propertyChanged: (bindable, oldValue, newValue) =>
{
((CheckBox)bindable).CheckedChanged?.Invoke(bindable, new CheckedChangedEventArgs((bool)newValue));
});
public static readonly BindableProperty TickColorProperty =
BindableProperty.Create(nameof(TickColor), typeof(Color), ypeof(CheckBox), Color.Default, BindingMode.TwoWay);
public static readonly BindableProperty CheckBoxBackgroundColorProperty = BindableProperty.Create(nameof(CheckBoxBackgroundColor), typeof(Color), typeof(CheckBox), Color.Default, BindingMode.TwoWay);
public EventHandler<CheckedChangedEventArgs> CheckedChanged;
public Color TickColor
{
get => (Color)GetValue(TickColorProperty);
set => SetValue(TickColorProperty, value);
}
public Color CheckBoxBackgroundColor
{
get => (Color)GetValue(CheckBoxBackgroundColorProperty);
set => SetValue(CheckBoxBackgroundColorProperty, value);
}
public bool Checked
{
get => (bool)GetValue(CheckedProperty);
set
{
if (Checked != value) SetValue(CheckedProperty, value);
}
}
}
public class CheckedChangedEventArgs : EventArgs
{
public CheckedChangedEventArgs(bool value)
{
Checked = value;
}
public bool Checked { get; }
}
}
Can anyone help me please?! 谁能帮我吗?
This class doesn't implement the UI of the checkbox. 此类未实现复选框的UI。 It's just a Xamarin.Forms class to create native Renderers with it.
它只是一个Xamarin.Forms类,用于使用它创建本机渲染器。 Usually I use a Switch when I need a Checkbox, but if a Switch is not good enough you either will have to look for the native renderers implementation on each platform or look for a cross-platform implementation using Xamarin.Forms' views.
通常,当需要复选框时,我会使用Switch,但是如果Switch不够好,您将不得不在每个平台上寻找本机呈示器实现,或者使用Xamarin.Forms的视图来寻找跨平台的实现。
And for your other question, the control you want is not a SwitchCell
, is simply a Switch
. 对于另一个问题,您想要的控件不是
SwitchCell
,而仅仅是Switch
。 If you want, here's the link to the documentation with examples: Switch Documentation . 如果需要,这里是带有示例的文档的链接: Switch Documentation 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.