首页 > 解决方案 > 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"/>

标签: c#xamlxamarinxamarin.forms

解决方案


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;


        }
    }

推荐阅读