【问题标题】:How do I refresh a tab on Xamarin.Forms app如何刷新 Xamarin.Forms 应用程序上的选项卡
【发布时间】:2020-03-03 04:29:24
【问题描述】:

我的应用程序运行非常顺利,我遇到的一个主要问题是我有一个保存按钮,一旦单击它就会将用户输入的值的实例保存在本地 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"/>

【问题讨论】:

  • 如果您使用的是 Binding,您只需更新您的 VM,您的 UI 就会刷新。如果不是,那么您将需要手动执行某些操作来更新 UI。
  • @Jason 我正在使用绑定,但不确定下一步是什么,我认为它会更新为使用 OnPropertyChanged
  • 您希望改变什么?您是否希望在 ListView 和/或其他内容中看到新值?
  • @Jason 我的计划是我的所有默认值都是 0,当用户插入值然后保存字段时,一个字段不会返回任何我想要的值,然后保存字段将返回默认值
  • 您发布的代码似乎没有使用绑定,尽管您的 cmets 说您这样做。没有看到更多的代码很难说。根据我所看到的,您应该在保存时将proName.Text 等设置回默认值。

标签: c# xaml xamarin xamarin.forms


【解决方案1】:

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;


        }
    }

【讨论】:

  • 很高兴听到您现在已经解决了您的问题,请记得将您的回复标记为答案,谢谢。
  • @CherryBu-MSFT 它说我需要等待 2 天才能接受我自己的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多