0

我有以下转换器:

公共类 InverseBooleanConverter : IValueConverter { #region IValueConverter 成员

    public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        try
        {
            if (targetType != typeof(bool))
                throw new InvalidOperationException("The target must be a boolean");
        }
        catch(Exception ex)
        {
            int x = 1;
        }
        return !(bool)value;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }

    #endregion
}

我正在尝试像这样使用它来控制基于代码隐藏属性“CanShowResults”的列表视图的 IsVisible 和页面上的活动指示器:

        <ListView x:Name="listView" BackgroundColor="White" SeparatorColor="#e0e0e0" IsVisible="False">
            <ListView.Triggers>
                <MultiTrigger TargetType="ListView">
                    <MultiTrigger.Conditions>
                        <BindingCondition Binding="{Binding Source={x:Reference retrievingActivity}, Path=IsRunning, Converter={StaticResource boolInvert}}" Value="true" />
                        <BindingCondition Binding="{Binding Path=CanShowResults}" Value="True" />
                    </MultiTrigger.Conditions>
                    <Setter Property="IsVisible" Value="True" />
                </MultiTrigger>
            </ListView.Triggers>
            <ListView.ItemTemplate>

. . . . .

我在 Convert 方法中遇到异常。我已经搜索了文档,有没有人看到我做错了什么?

4

1 回答 1

0

targetType用于指出要将值转换为的类型。并且没有必要将它传递给您的 IValueConverter 类。它会根据要将其转换为的类型自动设置。

例如,如果您在 Lable 的 Text 上使用 IValueConverter,targetType则为System.String. 你targetType总是System.Object因为你在BindingCondition.

如果您想手动指出类型,您可以尝试ConverterParameter

<BindingCondition Binding="{Binding Source={x:Reference retrievingActivity}, Path=IsRunning, Converter={StaticResource boolInvert}, ConverterParameter={x:Type x:Boolean}}" Value="true" />

然后在IValueConverter类中检索它,如:

try
{
    if ((Type)parameter != typeof(bool))
        throw new InvalidOperationException("The target must be a boolean");
}
catch (Exception ex)
{
    int x = 1;
}
return !(bool)value;

此外,我们if (value is bool)直接使用了杰森所说的。

于 2019-06-11T08:32:43.877 回答