我如何刷新Xamarin.Forms应用程序上的选项卡

问题描述 投票:0回答:1

我的应用程序运行非常平稳,我遇到的一个主要问题是我有一个保存按钮,单击该按钮会将用户输入的值的实例保存在本地SQLite数据库中。当我切换到其他选项卡时,它不会保存...但是,如果我关闭了该应用程序,则重新打开它,就好像没有通过应用程序的单击刷新一样。另外,当我点击保存时,我希望用户单击保存按钮后填写的所有输入框都为空白。

 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;
            }
        }

以上是我的代码,用于保存按钮并出现。下面是xaml保存按钮(不确定是否需要)

 <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# xaml xamarin xamarin.forms
1个回答
0
投票

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;


        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.