复制框在发布模式UWP(Xamarin Forms)中不可见

时间:2017-06-23 09:14:09

标签: xamarin xamarin.forms xamarin.uwp xlabs

我对xamarin.forms完全陌生。

我使用XLabs库在我的PCL项目中添加了复选框(Xamarin Forms)。

当我在调试模式下运行我的应用程序UWP ARM时没有错误,但是当我在发布模式下运行应用程序时,复选框从未显示。

我需要配置任何设置吗?

1 个答案:

答案 0 :(得分:4)

正如@hugo所说,不再维护XLabs库。它可能不适用于较新版本的Xamarin.Forms。根据您的要求,您可以使用Switch控件来替换复选框或使用自定义复选框控件。以下代码实现了一个简单的复选框。有关更多信息,请参阅Introduction to Custom Renderers

<强> CustomCheckBox.cs

public class CustomCheckBox : View
{
    public static readonly BindableProperty CheckedProperty =
    BindableProperty.Create("Checked", typeof(bool), typeof(CustomCheckBox), default(bool));

    public bool Checked
    {
        get { return (bool)GetValue(CheckedProperty); }
        set { SetValue(CheckedProperty, value); }
    }

}

<强> CustomCheckBoxRenderer.cs

[assembly: ExportRenderer(typeof(CustomCheckBox), typeof(CustomCheckBoxRenderer))]
namespace LabsTest.UWP
{
    public class CustomCheckBoxRenderer : ViewRenderer<CustomCheckBox, Windows.UI.Xaml.Controls.CheckBox>
    {
        protected override void OnElementChanged(ElementChangedEventArgs<CustomCheckBox> e)
        {
            base.OnElementChanged(e);
            if (Control == null)
            {
                SetNativeControl(new Windows.UI.Xaml.Controls.CheckBox());
            }
            if (Control != null)
            {
                Control.IsChecked = Element.Checked;
            }
        }
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            if (e.PropertyName == nameof(Element.Checked))
            {
                UpdateStatus();
            }
        }
        private void UpdateStatus()
        {
            Control.IsChecked = Element.Checked;
        }
    }
}

<强>用法

<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
    <local:CustomCheckBox x:Name="MyCheckBox" Checked="True">
    </local:CustomCheckBox>
</StackLayout>

enter image description here