Xamarin.forms 选择器值保存到SQLite
我和我的提货员有问题。我跟随了这篇文章 我得到“未处理的异常:System.Reflection.TargetInvocationException:调用的目标已引发异常。” 这是我的模型Xamarin.forms 选择器值保存到SQLite,xamarin.forms,Xamarin.forms,我和我的提货员有问题。我跟随了这篇文章 我得到“未处理的异常:System.Reflection.TargetInvocationException:调用的目标已引发异常。” 这是我的模型 public class AdLogEntry { [PrimaryKey, AutoIncrement] public int Id { get; set; } public string SellerName { get; set; }
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>
公共类广告条目
{
[主密钥,自动增量]
公共int Id{get;set;}
公共字符串SellerName{get;set;}
公共字符串ImagePath{get;set;}
//公共ImageSource ImageName{get;set;}
公共字节[]OriginalImage{get;set;}
公共字符串信息{get;set;}
公共字符串部分{get;set;}
公共字符串动物分类{get;set;}
公共字符串名称{get;set;}
公共字符串说明{get;set;}
公共字符串Price{get;set;}
公共日期时间加载日期{get;}
公共日期时间日期{get;set;}
公共日期选择器GetDatePicker{get;}
公共字符串位置{get;set;}
公共整数{get;set;}
}
这是我的密码
已单击私有异步无效保存(对象发送方,事件参数e)
{
等待SaveAdLog();
}
//待办事项对所有人都是无效的
//必填字段的待办事项标记
专用异步任务SaveAdLog()
{
如果(!string.IsNullOrWhiteSpace(NameEntry.Text)| |(!string.IsNullOrWhiteSpace(PriceEntry.Text)| |(!string.IsNullOrWhiteSpace(LocationEntry.Text)))
{
AdLogEntry AdLogEntry=新的AdLogEntry
{
Location=LocationEntry.Text,
Price=PriceEntry.Text,
Name=namentry.Text,
Section=(字符串)SectionPicker.SelectedItem,
AnimalCategory=(字符串)CategoryPicker.SelectedItem,
};
_adService.CreateAddLogEntry(adLogEntry);
等待显示警报(LabelCZ.alertThankyu、LabelCZ.AlertSpace、LabelCZ.AlertOk);
}
其他的
{
等待显示警报(LabelCZ.AlertRequired、LabelCZ.AlertRequiredPlease、LabelCZ.AlertOk);
};
}
也许你们能看到一些错误 上面的代码似乎没有什么问题,我测试了你的代码(只需将Piker ItemSource替换为固定字符串数组),它可以工作,尝试检查你的XF版本,更新并确保在PCL项目和android项目或ios项目中的版本相同这与你之前遇到的问题完全相同。您正试图在数据库中创建一个
DatePicker
列,SQLite不知道这是什么。SQLite只能处理诸如int、string、DateTime等基本类型。这不是同一个选择器,我还没有使用date pciker。但是谢谢你的回答,我会确保我把它分类。这些选择器是字符串部分和动物分类Jason正在谈论“公共数据包GETDATEPICKER {GET;}”你应该考虑把它改为String @ Leo Zhu - MSFT HI,它现在工作,谢谢。如果您将其添加为answr,我可以将其标记为>]