저는 Xamarin.forms에서 새로 왔으며 목록에서 만드는 여러 개의 CheckBox를 사용하려고합니다.Xamarin.forms에서 CheckBox를 사용하는 방법?
저는 CheckBox가 Xamarin.forms에 존재하지 않는다는 것을 알고 있으므로 컨트롤을 만들기 위해 인터넷에서 찾은 클래스를 만들었습니다.
CheckBox를 만들려고해도 볼 수 없습니다. I 체크 박스 만들 때이 코드입니다 :
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);
}
}
을 그리고 이것은하여 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; }
}
}
사람이 나에게 제발 도와 드릴까요?!
온라인으로 찾은 원래 클래스 게시하기 – Greggz
그리고 Xaml과 마찬가지로 – Greggz
이 클래스는 확인란의 UI를 구현하지 않습니다. 그것은 네이티브 렌더러를 만드는 Xamarin.Forms 클래스입니다. 보통 체크 박스가 필요할 때 스위치를 사용하지만 스위치가 충분하지 않으면 각 플랫폼에서 네이티브 렌더러 구현을 찾거나 Xamarin.Forms의 뷰를 사용하여 크로스 플랫폼 구현을 찾아야합니다. –