ScottishRoss91
ScottishRoss91

Reputation: 48

Xamarin.Forms: Error Specified cast is not valid when using Picker

I thought I had fixed this, but it doesn't seem to be working anymore. I have a picker that looks like this:

                        <StackLayout Padding="15">
                            <Label Text="Choose a Victory"
                                   FontSize="Subtitle"
                                   FontAttributes="Bold"
                                   TextColor="{StaticResource TitleFontColour}"
                                   FontFamily="{StaticResource MontSerrat}"/>
                            <Picker x:Name="quickPicker" 
                                    Title="- Select -"
                                    TitleColor="White"
                                    HorizontalOptions="FillAndExpand"
                                    TextColor="#8759D7"
                                    FontFamily="{StaticResource MontSerrat}"
                                    ItemDisplayBinding="{Binding Desc}"
                                    SelectedItem="{Binding SelectedDesc}">
                            </Picker>
                        </StackLayout>

The picker is based on:

        public Task<List<QuickVictories>> GetQuickVictoriesAsync()
        {
            return _database.Table<QuickVictories>().OrderByDescending(x => x.DisplaySeq).ToListAsync();
        }

My Model is:

MainModel.cs

    public class QuickVictories
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        [MaxLength (140)]
        public string Desc { get; set; }
        [AutoIncrement]
        public int DisplaySeq { get; set; }
    }

My look behind AddVictory.xaml.cs

protected override async void OnAppearing()
        {
            base.OnAppearing();

            quickPicker.ItemsSource = await App.Database.GetQuickVictoriesAsync();
        }

        async void OnSaveButtonClicked(object sender, EventArgs e)
        {

            var selectedQuickVictory = quickPicker.SelectedItem as QuickVictories;
            var quickVictory = selectedQuickVictory.Desc;

            var victory = new TheVictory()
            {
                Title = title.Text ?? quickVictory ?? "No title",
                Quick = quickVictory ?? "N/A",
                Details = details.Text ?? "No details were entered.",
                Date = DateTime.UtcNow
            };

            await App.Database.SaveVictoryAsync(victory);

            await DisplayAlert(
                "You have just celebrated a Victory!",
                "Your Victory has been saved for future celebrations.",
                "Woohoo!"
            );

            await Navigation.PopAsync();
        }

Whenever I try to save using OnSaveButtonClicked I get the error specifically on this line: var quickVictory = selectedQuickVictory.Desc;

This method is currently working on my production version of the app that is live in the Google Play store, so I am very confused about why this isn't working anymore.

Does anyone have any idea where I can look to try and fix this because I'm fresh out of ideas.

The goal, to be clear, is that I get the Desc from the QuickVictories model. I have tried casting the object to string, but it doesn't seem to be working since moving to the picker being the return of a sqlite query.

Edit:

Uploading a pic of the SelectedItem debug.

SelectedItem Debug Menu

Upvotes: 0

Views: 237

Answers (1)

Jason
Jason

Reputation: 89214

you need to check if SelectedItem is null

if (picker.SelectedItem == null)

and display a message to the user, or whatever is appropriate for your use case

Upvotes: 1

Related Questions