【问题标题】:Xamarin forms: Getting NotImplementedExceptionXamarin 表单:获取 NotImplementedException
【发布时间】:2019-03-06 12:21:12
【问题描述】:

我的列表视图中有一个开关。在 xaml 中,为 IsToggled 属性添加了一个转换器:

<Switch
          IsToggled="{Binding userProfileTO.userId, Converter={StaticResource isToggledConverter}}"
          HorizontalOptions="EndAndExpand"
          VerticalOptions="CenterAndExpand"/>

转换器代码:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    bool toggle = false;
    // My Codes
    return toggle;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    throw new NotImplementedException();
}

运行此代码时在 ConvertBack 上获取 NotImplementedException。

Exception thrown: 'System.NotImplementedException' in Myprojectname.dll
An exception of type 'System.NotImplementedException' occurred in Myprojectname.dll but was not handled in user code
The method or operation is not implemented.

【问题讨论】:

  • 你没有实现 convertback 方法,所以你会得到一个 NotImplementedException 是正常的。
  • 你显式地抛出了一个 NotImplementedException,所以你当然会得到一个 NotImplementedException。它正在做你告诉它做的事情。

标签: xamarin.forms uiswitch notimplementedexception


【解决方案1】:

IsToggled 属性的默认绑定类型是“Two-way”。 这就是您的 ConvertBack 函数被调用的原因。 您可以简单地删除

throw new NotImplementedException();

在您的 ConvertBack 方法中,一切都会正常工作。

或者如果您不想这样做,您可以将绑定模式显式设置为One-way

【讨论】:

  • 如果我删除该代码,我可以提供什么来代替它? ConvertBack 方法应该返回值,return false 就够了吗?
  • 这样会让你的代码容易出错,最好切换到单向绑定模式。
猜你喜欢
  • 2016-05-26
  • 1970-01-01
  • 2019-08-15
  • 1970-01-01
  • 2020-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-01
相关资源
最近更新 更多