我想在IOS上显示方形复选框,iPad在xamarin形式

cwtwac6a  于 2022-12-07  发布在  iOS
关注(0)|答案(2)|浏览(130)
This Below is my IOS Page .I want to show this type of check box on iOS in xamarin form. I have use Xamarin 5.0 version. When I am try to use check element in iOS show rounded check box but I want square check box.

 <CheckBox  x:Name="chkRememberMe"  BackgroundColor="#7E7E7E" SizeChanged="20" IsChecked="False" CheckedChanged="ChkRememberMe_OnCheckedChanged" ></CheckBox>

这是我的复选框代码,我可以做什么,在iOS的一个方形复选框。我是完全新的xamarin.forms,我需要添加一个复选框,单选按钮和下拉列表。我尝试了一些样本,从网,但我不能得到的复选框。有人能帮我实现。控件。复选框创建复选框的ios和andorid在Xamarin的形式。现在我得到了复选框,但我无法读取值,无论它是否选中。

62lalag4

62lalag41#

您可以使用nuget的插件**Xamarin.Forms.InputKit**。
第一个
如果要自定义复选框的样式,可以选中https://github.com/enisn/Xamarin.Forms.InputKit/wiki/CheckBox

mbyulnm0

mbyulnm02#

如果您不想在其他平台上更改复选框,则可以使用自定义渲染器仅更改其在iOS上的外观。
将共享项目中的CheckBox子类化:public class CustomCheckBox : CheckBox {}
并将自定义渲染器添加到iOS项目:

using CoreGraphics;
using UIKit;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using YourApp.iOS;
using YourApp.Controls; // namespace of your CustomCheckBox
using System.ComponentModel;

[assembly: ExportRenderer(typeof(CustomCheckBox), typeof(CustomCheckBoxRenderer))]
namespace YourApp.iOS
{
    public class CustomCheckBoxRenderer : ViewRenderer<CustomCheckBox, UIView>
    {
        private const float SideLength = 18f;
        private bool firstTime = true;

        protected override void OnElementChanged(ElementChangedEventArgs<CustomCheckBox> e)
        {
            base.OnElementChanged(e);
            if (Control == null)
            {
                var checkBox = new UIView();
                SetNativeControl(checkBox);
            }
        }
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            if (e.PropertyName == CustomCheckBox.IsCheckedProperty.PropertyName || firstTime)
            {
                SetNeedsDisplay();
            }
        }

        public override void Draw(CGRect rect)
        {
            Element.Color.ToUIColor().SetStroke();
            if (Element.IsChecked)
            {
                var checkPath = new UIBezierPath();
                checkPath.MoveTo(new CGPoint(1 + .2 * SideLength, 1 + .475 * SideLength));
                checkPath.AddLineTo(new CGPoint(1 + .45 * SideLength, 1 + .675 * SideLength));
                checkPath.AddLineTo(new CGPoint(1 + .8 * SideLength, 1 + .275 * SideLength));
                checkPath.LineWidth = 3f;
                checkPath.Stroke();
            }

            var boxPath = UIBezierPath.FromRoundedRect(
                new CGRect(1f, 1f, SideLength, SideLength), UIRectCorner.AllCorners, new CGSize(2,2));
            boxPath.LineWidth = 2f;
            boxPath.Stroke();
            firstTime = false;
        }

        public override CGSize SizeThatFits(CGSize size)
        {
            return new CGSize(SideLength + 2, SideLength + 2);
        }
    }
}

相关问题