Xamarin :如何在通知调用中调用 IValueConverter



我有一个Listview(它与ObservableCollection相关联(,所有元素都基于IValueConverter进行启用/禁用计算。

以下是IValueConverter的代码...

public class StateCheckConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var result = false;
            if (value != null)
            {
                var element = value as Element;
                if (element.Status != Status.Pending)
                    result = true;
            }
            return result;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }

现在我收到了一个通知(来自消息中心(,并且其中一个元素的回调状态已更改。我能够更改元素的文本和值(例如标签,使用INotifyPropertyChanged的图像(。但是如何调用相应的 IValueConverter 并更新 ObservableCollection?

谢谢。

更新:

<ContentPage.Resources>
    <ResourceDictionary>
      <vm:StateCheckConverter x:Key="transmissionStateCheck" />
    </ResourceDictionary>
  </ContentPage.Resources>

<Label x:Name="lblLocked"
                         IsVisible="{Binding ., Converter={StaticResource transmissionStateCheck}, Mode=TwoWay}"
                         HorizontalTextAlignment="Center"
                         BackgroundColor="Gray"
                         Opacity="0.75"
                         Text="LOCKED"
                         TextColor="White"
                         FontSize="35"
                         />
好的一种方法

是更改绑定属性并绑定到Status本身:

<Label x:Name="lblLocked"
       IsVisible="{Binding Status, Converter={StaticResource transmissionStateCheck}}"
       HorizontalTextAlignment="Center"
       BackgroundColor="Gray"
       Opacity="0.75"
       Text="LOCKED"
       TextColor="White"
       FontSize="35"/>

当然,您还必须更改值转换器:

public class StateCheckConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var result = false;
        if (value is Status status)
        {
            if (status != Status.Pending)
                result = true;
        }
        return result;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

我希望它对:)有所帮助

此外,我还更改了您的代码中的一些内容。您无法将IsVisible与双向模式绑定,因此它将自动为单向模式。

也转换回来应该采取bool并返回Status这是不可能和不必要的,所以我删除了它。

最新更新