c# - Picker in Xamarin.Forms crashes on click - Stack Overflow

admin2025-04-16  3

I'm developing a Xamarin.Forms app, and I'm facing an issue where the Picker crashes immediately when clicked. This problem occurs on Samsung and Google Pixel devices, but it does not happen on Huawei devices. The Picker doesn't open at all, and I get the following error message:

Invalid cast from 'System.String' to 'XXXXXXXXXXXXX.DataModel.Entities.Sorte'

The data for the Picker comes from a SQLite database, which I am not allowed to modify. I know that the first entry in the Picker is intentionally empty, and it is required by design.

Here are the relevant code snippets: VM

public Sorte SelectedSorte
{
    get
    {
        return SelectedOrderItem?.SelectedTaxItem?.SelectedSorte;
    }
    set
    {
        if (value != null && SelectedOrderItem?.SelectedTaxItem != null && value != SelectedOrderItem?.SelectedTaxtem?.SelectedSorte)
        {
            SelectedOrderItem.SelectedTaxItem.SelectedSorte = value;
            RaisePropertyChanged();
            Validate();
        }
    }
}

Here View I have tried using TargetNullValue='' and FallbackValue='' in XAML for the SelectedItem binding and ItemDisplayBinding binding, but unfortunately, it did not solve the issue.

<StackLayout
    x:Name="Sorte"
    Margin="0,10,0,0"
    Spacing="5">
    
    <Frame
        Padding="0"
        BorderColor="Transparent"
        CornerRadius="7">
        
        <Picker 
            x:Name="pickerSelectedSorte"
            ItemDisplayBinding="{Binding Description}"
            ItemsSource="{Binding SelectedOrderItem.SelectedTaxItem.OcSorten}"
            SelectedItem="{Binding SelectedSorte, Mode=TwoWay}" />
        
    </Frame>
</StackLayout>
转载请注明原文地址:http://anycun.com/QandA/1744812569a87964.html