c# - How do I refresh a tab on Xamarin.Forms app
问题描述
My app is running very smoothly, a major issue I'm having is that I have a save button, once clicked it saves an instance of the values entered by the user in a SQLite database locally. When I change to my other tab, it doesn't save... but if I shut the app down then reopen it its there as if there is no on click refresh through the app. also when I hit save I would like all the entry boxes the user fills in to go blank after the save button is clicked.
void SaveButton_Clicked(object sender, System.EventArgs e)
{
MainWindowViewModel APR = new MainWindowViewModel()
{
ProductName = proName.Text,
TotalAPR = totAPR.Text,
Total = tot.Text,
Monthly = mon.Text
};
using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
{
conn.CreateTable<MainWindowViewModel>();
int rowsAdded = conn.Insert(APR);
}
}
protected override void OnAppearing()
{
base.OnAppearing();
using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
{
conn.CreateTable<MainWindowViewModel>();
var APRS = conn.Table<MainWindowViewModel>().ToList();
APRListView.ItemsSource = APRS;
}
}
Above is my code for save button & appearing. Below is the xaml for save button (not sure if needed)
<Button Text="Save"
TextColor="#FFA600"
BackgroundColor="#2B333F"
Clicked="SaveButton_Clicked"
FontSize="16"
Padding="10"
Grid.Column="1"
Grid.ColumnSpan="3"
Grid.Row="14"
Grid.RowSpan="2"
VerticalOptions="Center"
HorizontalOptions="Center"
Margin="0,0,0,-20"/>
解决方案
SaveButton_Clicked 例程中缺少代码
BindingContext = new MainWindowViewModel();
OnAppearing();
完整的代码应该是
void SaveButton_Clicked(object sender, System.EventArgs e)
{
MainWindowViewModel APR = new MainWindowViewModel()
{
ProductName = proName.Text,
TotalAPR = totAPR.Text,
Total = tot.Text,
Monthly = mon.Text
};
using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
{
conn.CreateTable<MainWindowViewModel>();
int rowsAdded = conn.Insert(APR);
}
DisplayAlert("Saved!", "Your APR has been saved!", "OK");
BindingContext = new MainWindowViewModel();
OnAppearing();
}
protected override void OnAppearing()
{
base.OnAppearing();
using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
{
conn.CreateTable<MainWindowViewModel>();
var APRS = conn.Table<MainWindowViewModel>().ToList();
APRListView.ItemsSource = APRS;
}
}
推荐阅读
- azure - 有关 Azure Redis 群集中的活动客户端连接的信息
- ios - 表格视图删除行和部分
- amazon-neptune - 将数据加载到 Amazon Neptune 数据库
- jsonata - 如何在确保结果仍然是 jsonata 中的数组的同时从数组中删除第一个元素?
- angular - Angular 9. 组件是 Sample 类,但不是 SampleExtended,尽管它是作为 SampleExtended 创建的
- rust - 了解生命周期:最大生命周期和“静态”
- c# - 批量更新行值 ASP.NET Core C# 控制台应用程序
- javascript - 如何在结果中获得正确的值?(计算器js)
- python - 读取图像并进行颜色更改(使用 Image 或 openCV)(Python)
- angular - this.sanitizer.bypassSecurityTrustResourceUrl 在使用 ngx-image-cropper 时不是一个函数