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.
quickPicker.SelectedItem
? – JasonSelectedItem="{Binding SelectedDesc}"
– Jason