【问题标题】:How to refresh ContentPage in pure button click handler如何在纯按钮单击处理程序中刷新 ContentPage
【发布时间】:2017-09-23 08:44:24
【问题描述】:

上下文

我想诊断一个绑定问题。在这里不暴露问题细节,我决定在我的 ContentPage 上放一个诊断按钮,并想以最纯粹最愚蠢的方式写一个刷新,只是为了看看,已经绑定的内容是否以这种方式显示。

问题

所以我有我的按钮和它的事件处理程序,我不知道如何调用刷新或类似的?

public partial class MainView : ContentPage
{
    public MainView()
    {
        InitializeComponent();
    }

    void OnButtonClicked(object sender, EventArgs args)
    {
        // I would like to refresh this contentpage here:
    }
}

【问题讨论】:

  • 有什么要刷新的?列表视图或某些组件必须存在,必须绑定到动态。数据对吗?
  • 是的,有一个列表,但是我想刷新整个页面
  • 如果你想刷新整个页面,为什么不触发一个事件并去app.cs页面分配一个全新的主页。
  • 您为什么不在OnButtonClicked 的页面上再次导航。您将获得一个新的页面实例,因此是一个“刷新”的页面。

标签: xamarin xamarin.forms


【解决方案1】:

最简单的方法是使用Navigation.PushAsyncApp.Current.MainPage = new MainView() 重置用户界面。

但是,如果您想简单地重新构建视图,那么我想调用 InitializeComponent 应该可以解决问题。

void OnButtonClicked(object sender, EventArgs args)
{
    var viewModel = BindingContext;

    BindingContext = null;
    InitializeComponent();

    BindingContext = viewModel;
}

注意:此代码仅重建视图(不是绑定数据)——假设您不想使用导航或设置MainPage 技巧——刷新 UI .

【讨论】:

  • 可能不需要 InitializeComponent。将 BindingContext 设置为 null,然后返回 viewModel,应该足以强制刷新所有绑定。
猜你喜欢
  • 2018-05-08
  • 2012-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-31
  • 2010-11-28
相关资源
最近更新 更多