【问题标题】:Xamarin.Forms - Disable SwitchCell OnPlatform Throws Exception for iOSXamarin.Forms - 禁用 SwitchCell OnPlatform 引发 iOS 异常
【发布时间】:2016-12-29 17:02:18
【问题描述】:

我正在尝试以这种方式在 Xamarin.Forms for iOS 中禁用 SwitchCell

<SwitchCell x:Name="mySC" Text="Foo" OnChanged="myFnc"/>
<SwitchCell.IsEnabled>
    <OnPlatform x:TypeArguments="x:Boolean">
            <OnPlatform.iOS>
                 false
             </OnPlatform.iOS>
            <OnPlatform.Android>
                 true
            </OnPlatform.Android>
        </OnPlatform>
</SwitchCell.IsEnabled>

当我在 iOS 模拟器上运行此代码时,会引发异常:

System.Reflection.TargetInvocationException:已抛出异常 通过调用的目标。 ---> System.NullReferenceException: 你调用的对象是空的 在 Xamarin.Forms.Cell.OnIsEnabledPropertyChanged(Xamarin.Forms.BindableObject 可绑定,System.Object oldvalue, System.Object newvalue) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Cells\Cell.cs:197

如果我将OnPlatform.iOS 的值设置为true,它就可以工作。如果我不使用OnPlatform 标签并为所有平台设置例如IsEnabled="false",它运行良好 - 假设是关于 iOS 模拟器中的错误。

【问题讨论】:

    标签: c# xamarin xamarin.ios xamarin.forms


    【解决方案1】:

    我认为问题在于SwitchCell.IsEnabled 属性必须在SwitchCell 元素内。您正在使用以下表格:

    <SwitchCell x:Name="mySC" Text="Foo" OnChanged="myFnc"/>
    

    在设置IsEnabled 属性之前关闭开关单元定义。试试这个:

    <SwitchCell Text="{Binding .}" >
          <SwitchCell.IsEnabled>
               <OnPlatform x:TypeArguments="x:Boolean">
                    <OnPlatform.iOS>
                         false
                    </OnPlatform.iOS>
                    <OnPlatform.Android>
                         true
                    </OnPlatform.Android>
               </OnPlatform>
          </SwitchCell.IsEnabled>
     </SwitchCell>
    

    以便在SwitchCell 的定义内而不是在它之外设置属性。我刚刚测试过,这在 iOS 和 Android 上都有效,但是在 iOS 上使用你的代码确实失败了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-22
      • 1970-01-01
      • 2016-08-31
      • 2016-11-22
      • 1970-01-01
      • 2018-12-09
      • 2017-05-18
      • 2014-09-07
      相关资源
      最近更新 更多