使用 Xamarin 在我的数据库 Slite 中插入寄存器,错误
Insert Register In my db Slite with Xamarin, Error
我正在将此数据插入到我的 Sqlite 数据库中,但是当我单击按钮时,应用程序立即关闭
private List<Tick> _tick;
public List<Tick> Tick
{
get { return _tick; }
set
{
_tick = value;
RaisePropertyChanged();
}
}
private string _title;
public string Titulo
{
get { return _title; }
set
{
_title = value;
RaisePropertyChanged();
}
}
private string _description;
public string Description
{
get { return _description; }
set
{
_description = value;
RaisePropertyChanged();
}
}
这就是方法
public ICommand SaveCommand => new Command(async () => await AddTiket());
private async Task AddTiket()
{
Tick ticks = new Tick
{
Titulo = Titulo,
Description = Description
};
App.CustomerRepository.AddOrUpdate(ticks);
Console.WriteLine(App.CustomerRepository.StatusMessage);
}
CustomerRepository 是我拥有所有方法的地方,当 StatusMessage 存储在数据库中时,它会在控制台中向我发送一条消息。
我只是给他们Title和Description,这样代码就不会那么长了
谢谢大家的帮助,学习中
根据您的描述,您在 AddTiket 事件中没有为 App.CustomerRepository.AddOrUpdate(ticks);
使用 Await
。你想在sqlite中添加Tick记录,看不到CustomerRepository class
,但我做了一个简单的例子,你可以看看
public class SQLiteHelper
{
SQLiteAsyncConnection db;
public SQLiteHelper(string dbPath)
{
db = new SQLiteAsyncConnection(dbPath);
db.CreateTableAsync<Tick>().Wait();
}
//Insert and Update new record
public Task<int> SaveItemAsync(Tick tick)
{
if (tick.Id != 0)
{
return db.UpdateAsync(tick);
}
else
{
return db.InsertAsync(tick);
}
}
//Delete
public Task<int> DeleteItemAsync(Tick tick)
{
return db.DeleteAsync(tick);
}
//Read All Items
public Task<List<Tick>> GetItemsAsync()
{
return db.Table<Tick>().ToListAsync();
}
//Read Item
public Task<Tick> GetItemAsync(int Id)
{
return db.Table<Tick>().Where(i => i.Id == Id).FirstOrDefaultAsync();
}
}
在App.xaml.cs
中创建静态SQLiteHelper
public partial class App : Application
{
static SQLiteHelper db;
public static SQLiteHelper SQLiteDb
{
get
{
if (db == null)
{
db = new SQLiteHelper(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Tickdb.db3"));
}
return db;
}
}
public App()
{
InitializeComponent();
在sqlite中添加数据,在ListView中显示所有数据。
<StackLayout>
<Button
x:Name="btn1"
Command="{Binding SaveCommand}"
Text="add tick" />
<ListView x:Name="listview1" ItemsSource="{Binding ticks}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Titulo}" />
<Label Text="{Binding Description}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
public partial class Page5 : ContentPage
{
public ICommand SaveCommand => new Command(async () => await AddTiket());
private ObservableCollection<Tick> _ticks;
public ObservableCollection<Tick> ticks
{
get { return _ticks; }
set
{
_ticks = value;
OnPropertyChanged("ticks");
}
}
public Page5()
{
InitializeComponent();
this.BindingContext = this;
}
private async Task AddTiket()
{
Tick tick = new Tick()
{
Titulo = "title 1",
Description = "this is description 1"
};
await App.SQLiteDb.SaveItemAsync(tick);
var tickList = await App.SQLiteDb.GetItemsAsync();
if (tickList != null)
{
ticks = new ObservableCollection<Tick>(tickList);
}
}
protected async override void OnAppearing()
{
base.OnAppearing();
//Get All Persons
var tickList = await App.SQLiteDb.GetItemsAsync();
if(tickList!=null)
{
ticks = new ObservableCollection<Tick>(tickList);
}
}
}
public class Tick
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Titulo { get; set; }
public string Description { get; set; }
}
我正在将此数据插入到我的 Sqlite 数据库中,但是当我单击按钮时,应用程序立即关闭
private List<Tick> _tick;
public List<Tick> Tick
{
get { return _tick; }
set
{
_tick = value;
RaisePropertyChanged();
}
}
private string _title;
public string Titulo
{
get { return _title; }
set
{
_title = value;
RaisePropertyChanged();
}
}
private string _description;
public string Description
{
get { return _description; }
set
{
_description = value;
RaisePropertyChanged();
}
}
这就是方法
public ICommand SaveCommand => new Command(async () => await AddTiket());
private async Task AddTiket()
{
Tick ticks = new Tick
{
Titulo = Titulo,
Description = Description
};
App.CustomerRepository.AddOrUpdate(ticks);
Console.WriteLine(App.CustomerRepository.StatusMessage);
}
CustomerRepository 是我拥有所有方法的地方,当 StatusMessage 存储在数据库中时,它会在控制台中向我发送一条消息。
我只是给他们Title和Description,这样代码就不会那么长了 谢谢大家的帮助,学习中
根据您的描述,您在 AddTiket 事件中没有为 App.CustomerRepository.AddOrUpdate(ticks);
使用 Await
。你想在sqlite中添加Tick记录,看不到CustomerRepository class
,但我做了一个简单的例子,你可以看看
public class SQLiteHelper
{
SQLiteAsyncConnection db;
public SQLiteHelper(string dbPath)
{
db = new SQLiteAsyncConnection(dbPath);
db.CreateTableAsync<Tick>().Wait();
}
//Insert and Update new record
public Task<int> SaveItemAsync(Tick tick)
{
if (tick.Id != 0)
{
return db.UpdateAsync(tick);
}
else
{
return db.InsertAsync(tick);
}
}
//Delete
public Task<int> DeleteItemAsync(Tick tick)
{
return db.DeleteAsync(tick);
}
//Read All Items
public Task<List<Tick>> GetItemsAsync()
{
return db.Table<Tick>().ToListAsync();
}
//Read Item
public Task<Tick> GetItemAsync(int Id)
{
return db.Table<Tick>().Where(i => i.Id == Id).FirstOrDefaultAsync();
}
}
在App.xaml.cs
中创建静态SQLiteHelper public partial class App : Application
{
static SQLiteHelper db;
public static SQLiteHelper SQLiteDb
{
get
{
if (db == null)
{
db = new SQLiteHelper(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Tickdb.db3"));
}
return db;
}
}
public App()
{
InitializeComponent();
在sqlite中添加数据,在ListView中显示所有数据。
<StackLayout>
<Button
x:Name="btn1"
Command="{Binding SaveCommand}"
Text="add tick" />
<ListView x:Name="listview1" ItemsSource="{Binding ticks}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Titulo}" />
<Label Text="{Binding Description}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
public partial class Page5 : ContentPage
{
public ICommand SaveCommand => new Command(async () => await AddTiket());
private ObservableCollection<Tick> _ticks;
public ObservableCollection<Tick> ticks
{
get { return _ticks; }
set
{
_ticks = value;
OnPropertyChanged("ticks");
}
}
public Page5()
{
InitializeComponent();
this.BindingContext = this;
}
private async Task AddTiket()
{
Tick tick = new Tick()
{
Titulo = "title 1",
Description = "this is description 1"
};
await App.SQLiteDb.SaveItemAsync(tick);
var tickList = await App.SQLiteDb.GetItemsAsync();
if (tickList != null)
{
ticks = new ObservableCollection<Tick>(tickList);
}
}
protected async override void OnAppearing()
{
base.OnAppearing();
//Get All Persons
var tickList = await App.SQLiteDb.GetItemsAsync();
if(tickList!=null)
{
ticks = new ObservableCollection<Tick>(tickList);
}
}
}
public class Tick
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Titulo { get; set; }
public string Description { get; set; }
}