【问题标题】:Picker value saving to SQLite选择器值保存到 SQLite
【发布时间】:2020-03-12 16:19:37
【问题描述】:

我的选择器有问题。我关注了这个帖子Saving and reading Picker values from SQLite - Xamarin.Forms

我得到“未处理的异常:System.Reflection.TargetInvocationException:异常已被调用的目标抛出。”

这是我的模特

 public class AdLogEntry
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        public string SellerName { get; set; }
        public string ImagePath { get; set; }
        //public ImageSource ImageName { get; set; }
        public byte[] OriginalImage { get; set; }
        public string Info { get; set; }
        public string Section { get; set; }
        public string AnimalCategory { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Price { get; set; }
        public DateTime LoadedDate { get; }
        public DateTime Date { get; set; }
        public DatePicker GetDatePicker { get;  }
        public string Location { get; set; }
        public int Age { get; set; }

    }

this is my code

private async void Save_Clicked(object sender, EventArgs e)
        {
            await SaveAdLog();

        }
        //TODO IsNullOR for all
        //TODO Marks for required fields
        private async Task SaveAdLog()
        {
            if (!string.IsNullOrWhiteSpace(NameEntry.Text) || (!string.IsNullOrWhiteSpace(PriceEntry.Text) || (!string.IsNullOrWhiteSpace(LocationEntry.Text)  )))
            {
                AdLogEntry adLogEntry = new AdLogEntry
                {
                    Location = LocationEntry.Text,
                    Price = PriceEntry.Text,
                    Name = NameEntry.Text,

                    Section = (string)SectionPicker.SelectedItem,
                    AnimalCategory = (string)CategoryPicker.SelectedItem,

                };
                _adService.CreateAddLogEntry(adLogEntry);
                await DisplayAlert(LabelCZ.AlertThankYou, LabelCZ.AlertSpace, LabelCZ.AlertOk);
            }
            else
            {
                await DisplayAlert(LabelCZ.AlertRequired, LabelCZ.AlertRequiredPlease, LabelCZ.AlertOk);
            };

        }


<Grid Grid.Row="0" RowSpacing="12">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <Entry  x:Name="NameEntry" Placeholder ="{x:Static resources:LabelCZ.LabelWhatAreYouSelling}" Grid.Row="1"/>
                                    <Picker x:Name="SectionPicker"  Grid.Row="2"  Title="{x:Static resources:LabelCZ.LabelSection}" ItemsSource="{Binding Sections}" ItemDisplayBinding="{Binding SectionName}" SelectedItem="{Binding Section}"/>
                                    <Picker x:Name="CategoryPicker" Grid.Row="3"  Title="{x:Static resources:LabelCZ.LabelCategory}"  ItemsSource="{Binding AnimalCategories}" ItemDisplayBinding="{Binding AnimalKind}" SelectedItem="{Binding AnimalCategory}"/>
                                    <Picker x:Name="PetsCategoryPicker" Grid.Row="4" Title="{x:Static resources:LabelCZ.LabelPetsCategory}" ItemsSource="{Binding CatBreeds}" ItemDisplayBinding="{Binding CatBreedKind}" />
                                <Entry  x:Name="PriceEntry" Grid.Row="5" Placeholder ="{x:Static resources:LabelCZ.LabelPrice}" FontSize="{ StaticResource SubTitleSize }"/>
                                <Entry  x:Name="LocationEntry" Grid.Row="6" Placeholder= "{x:Static resources:LabelCZ.LabelLocation}" FontSize="{ StaticResource SubTitleSize }" />
                                <Entry x:Name="PickUpDate" Grid.Row="7" Placeholder ="{x:Static resources:LabelCZ.LabelPickUPDay}" FontSize="{ StaticResource SubTitleSize }"/>


                            </Grid>

也许你们能看出一些错误?

【问题讨论】:

  • 这是您之前遇到的完全相同的问题。您正在尝试在数据库中创建 DatePicker 列,而 SQLite 不知道那是什么。 SQLite 只能使用原始类型,如 int、string、DateTime 等。
  • 那不是同一个选择器,我还没有使用日期 pciker。但谢谢你的回答,我会确保我解决这个问题。
  • 这些选择器是字符串部分和动物类别
  • Jason 正在谈论`public DatePicker GetDatePicker { get; } ` 你应该考虑把它改成字符串
  • @Leo Zhu - MSFT 嗨,它现在可以工作了,谢谢。如果您将其添加为 answr 我可以标记它>]

标签: xamarin.forms


【解决方案1】:

您上面的代码似乎没有任何问题,我测试了您的代码(只需将 Piker ItemSource 替换为固定的字符串数组),它可以工作,尝试检查您的 XF 版本,更新并确保版本相同在 PCL 项目和 android 项目或 ios 项目中都有

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多