简体   繁体   English

Xamarin.Forms:使用 Picker 时错误指定的强制转换无效

[英]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 调试的图片。

SelectedItem Debug Menu 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.

相关问题 Xamarin.Forms 绑定指定的强制转换无效 - Xamarin.Forms Binding Specified cast is not valid 在 Xamarin Forms 中获取 SelectedItem 值时,选择器错误“指定的转换无效” - Picker error “Specified cast is not valid” when getting the SelectedItem value in Xamarin Forms 为什么我在尝试使用 viewModel 中的列表填充 xamarin.forms 中的列表视图时出现错误:“Specified Cast is not valid”? - Why do I get the error: " Specified Cast is not valid" when trying to populate my listview in xamarin.forms with a list in my viewModel? 指定的演员表无效 - Xamarin Forms - Specified cast is not valid - Xamarin Forms 指定的强制转换无效(xamarin 形式) - Specified cast is not valid (xamarin forms) 指定的转换在 Xamarin Forms 中无效 - Specified cast is not valid in Xamarin Forms 单击 Pin 时尝试从 API 加载数据 (CustomMap) 错误:指定的转换无效。 - xamarin.forms.maps - Trying to load data from API when Pin is clicked (CustomMap) error: Specified cast is not valid. - xamarin.forms.maps 验证 xamarin.forms 选择器 - Validating xamarin.forms picker Xamarin.Forms中Picker上的OnChangeListener - OnChangeListener on Picker in Xamarin.Forms 为什么从 sqlite db 中选择会显示错误“指定的强制转换无效”。 在 Xamarin 表单中? - Why does select from sqlite db shows error 'Specified cast is not valid.' in Xamarin Forms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM