[英]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主模型.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我对 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;
每当我尝试使用 OnSaveButtonClicked 进行保存时,我都会在此行上特别收到错误消息: 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.这种方法目前正在我在 Google Play 商店中运行的应用程序的生产版本上运行,所以我很困惑为什么这不再起作用了。
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.明确地说,目标是我从 QuickVictories 模型中获得 Desc。 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.我曾尝试将对象转换为字符串,但自从移动到选择器是 sqlite 查询的返回后,它似乎不起作用。
Edit:编辑:
Uploading a pic of the SelectedItem debug.上传 SelectedItem 调试的图片。
you need to check if SelectedItem
is null您需要检查SelectedItem
是否为空
if (picker.SelectedItem == null)
and display a message to the user, or whatever is appropriate for your use case并向用户显示一条消息,或任何适合您的用例的消息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.