在 ValueConverter 中获取 NullReferenceException

问题描述 投票:0回答:2

我有以下 IValueConverter、StringCaseConverter.cs:

internal sealed class StringCaseConverter : IValueConverter
{
    public bool IsUpperCase { private get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo language)
    {
        var stringValue = value.ToString();

        return this.IsUpperCase ? stringValue.ToUpperInvariant() : stringValue.ToLowerInvariant();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo language)
    {
        return value;
    }
}

我在App.xaml中注册了它:

<Application.Resources>
    <ResourceDictionary>
        <converters:StringCaseConverter x:Key="StringToLowerCaseConverter" IsUpperCase="False"/>
        <converters:StringCaseConverter x:Key="StringToUpperCaseConverter" IsUpperCase="True"/>
    </ResourceDictionary>
</Application.Resources>

并在 MainPage.xaml 中使用它:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="App1.MainPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">

    <StackLayout Orientation="Horizontal" HorizontalOptions="Center">
        <CollectionView HorizontalOptions="Center">
            <CollectionView.ItemsSource>
                <x:Array Type="{x:Type x:String}">
                    <x:String>A</x:String>
                    <x:String>B</x:String>
                    <x:String>C</x:String>
                    <x:String>D</x:String>
                    <x:String>E</x:String>
                    <x:String>F</x:String>
                    <x:String>G</x:String>
                    <x:String>H</x:String>
                </x:Array>
            </CollectionView.ItemsSource>

            <CollectionView.ItemsLayout>
                <LinearItemsLayout Orientation="Horizontal" ItemSpacing="20"/>
            </CollectionView.ItemsLayout>

            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout Orientation="Horizontal" HorizontalOptions="End" VerticalOptions="Center">
                        <Label Text="{Binding ., Converter={StaticResource StringToLowerCaseConverter}}" HorizontalOptions="Center"/>
                    </StackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>
</ContentPage>

当我运行应用程序时,我在 StringCaseConverter.cs (

var stringValue = value.ToString();
) 中收到 NullReferenceExecption,因为
value
方法的
Convert()
参数为 null。

我错过了什么?

xamarin.forms ivalueconverter
2个回答
0
投票

只需要做一个空值判断即可。

 public object Convert(object value, Type targetType, object parameter, CultureInfo language)
    {
        if (string.IsNullOrEmpty((string)value))
        {
            return null;
        }
        var stringValue = value.ToString();

        return this.IsUpperCase ? stringValue.ToUpperInvariant() : stringValue.ToLowerInvariant();
    }

0
投票

当前异常:

 at Xamarin.Forms.Xaml.ValueConverterProvider.Convert (System.Object value, System.Type toType, System.Func`1[TResult] minfoRetriever, System.IServiceProvider serviceProvider) [0x0000f] in D:\a\1\s\Xamarin.Forms.Core\Xaml\ValueConverterProvider.cs:17 

在 D:\s\Xamarin.Forms.Core\Interactivity\BindingCondition.cs:79 中的 Xamarin.Forms.BindingCondition.EqualsToValue (System.Object other) [0x00025] 在 D:\s\Xamarin.Forms.Core\Interactivity\BindingCondition.cs:89 中的 Xamarin.Forms.BindingCondition.OnBoundPropertyChanged (Xamarin.Forms.BindableObject 可绑定、System.Object oldValue、System.Object newValue)[0x00008] 在 Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty 属性、Xamarin.Forms.BindableObject+BindablePropertyContext 上下文、System.Object 值、System.Boolean currentApplying、Xamarin.Forms.Internals.SetValueFlags 属性、System.Boolean 静默)[ 0x00120] 在 D:\s\Xamarin.Forms.Core\BindableObject.cs:512 在 Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty 属性、System.Object 值、Xamarin.Forms.Internals.SetValueFlags 属性、Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes)[0x00173] 在 D:\s\ Xamarin.Forms.Core\BindableObject.cs:446 在 Xamarin.Forms.BindingExpression.ApplyCore (System.Object sourceObject、Xamarin.Forms.BindableObject 目标、Xamarin.Forms.BindableProperty 属性、System.Boolean fromTarget) [0x00226] 在 D:\s\Xamarin.Forms.Core\ BindingExpression.cs:160 在 D:\s\Xamarin.Forms.Core\BindingExpression.cs:56 中的 Xamarin.Forms.BindingExpression.Apply (System.Boolean fromTarget) [0x0003e] 在 D:\s\Xamarin.Forms.Core\BindingExpression.cs:773 中的 Xamarin.Forms.BindingExpression+BindingExpressionPart.b__50_0 () [0x00000] 在 Xamarin.Forms.BindingExpression+BindingExpressionPart.PropertyChanged (System.Object 发送者,System.ComponentModel.PropertyChangedEventArgs args) [0x000cb] 在 D:\s\Xamarin.Forms.Core\BindingExpression.cs:784 在 D:\s\Xamarin.Forms.Core\BindingExpression.cs:675 中的 Xamarin.Forms.BindingExpression+WeakPropertyChangedProxy.OnPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x00012] 在(包装器委托调用).invoke_void_object_PropertyChangedEventArgs(对象,System.ComponentModel.PropertyChangedEventArgs)

解决方案: 我有两个带有 Setter 的 DataTrigger 作为一个控件。我删除了第二个 DataTrigger。完成了。

© www.soinside.com 2019 - 2024. All rights reserved.