【问题标题】:Checkboxes not visible in release mode UWP (Xamarin Forms)复选框在发布模式 UWP(Xamarin 窗体)中不可见
【发布时间】:2017-11-26 19:21:36
【问题描述】:

我对 xamarin.forms 完全陌生。

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

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

我需要配置什么设置吗?

【问题讨论】:

  • 这个库好像有问题,请看this 获取其他方法。
  • 我尝试了这些解决方案,但它们对我没有帮助。
  • XLab 已弃用。不要使用它。对于复选框,为什么不使用 Switch 控件?
  • 因为我需要为一类问题添加答案列表,而 Switch 控件不是该项目的解决方案。

标签: xamarin xamarin.forms xamarin.uwp xlabs


【解决方案1】:

正如@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>

【讨论】:

  • 最后我添加了一个自定义渲染器。谢谢!
  • 我正在使用这个实现,但是当用户选中复选框时 OnElementPropertyChanged 永远不会触发属性 Checked,而只会触发 IsFocused 。
  • 我终于解决了在OnElementChanged方法中添加事件订阅Control.Checked += (s,r)=&gt; { Element.Checked = true; };Control.Unchecked += (s, r) =&gt; { Element.Checked = false; };
猜你喜欢
  • 2021-02-07
  • 2019-01-15
  • 1970-01-01
  • 1970-01-01
  • 2021-08-07
  • 1970-01-01
  • 2011-07-02
  • 2019-11-18
  • 1970-01-01
相关资源
最近更新 更多