如果导航页面已经位于堆栈顶部,如何从左侧菜单母版页刷新导航页面?

时间:2016-05-08 20:53:16

标签: c# android xamarin xamarin.forms

所以,我有一个母版页,其中包含您从第A页上的汉堡包图标访问的左侧菜单。

在左侧菜单上,我有一个名为"刷新页面A"当我单击该按钮时,我希望页面A的内容已经在堆栈顶部进行刷新/重新加载。

但是,我遇到了问题,因为构造函数onResume和onAppearing没有被调用。我不确定如何让左侧菜单按钮刷新第A页。

在我的LeftMenu课程中,我有类似

的内容
In [385]: cat data_sample.tsv
This is a new line
This is another line of text
And this is the last line of text in this file

In [386]: lines = []

In [387]: for line in pd.read_csv('./data_sample.tsv', encoding='utf-8', header=None, chunksize=1):
    lines.append(line.iloc[0,0])
   .....:     

In [388]: print(lines)
['This is a new line', 'This is another line of text', 'And this is the last line of text in this file']

这就是我导航到页面的方式:

this.Add(new MenuItem()
{
    Title = "Refresh Page A",
    Icon = "icon.png",
    SelectedIconSource = "icona.png",
    TargetType = typeof(PageA),
    Tcolor = COLOR_MENU,
    SelectedTColor = Color.White,
    MenuType=MenuType.PageAType,
    ToolbarResource = Resource.Layout.mainpage_toolbar
});

那我该怎么刷新页面呢?我有一个我可以使用的刷新方法,但应该从哪里调用?

1 个答案:

答案 0 :(得分:5)

所以我看到了几种方法。

  1. 通过保留对该页面的引用来跟踪“详细信息”页面中当前显示的内容。通过这种方式,您可以检测它是否属于您期望的类型并调用方法而不是再次尝试推送该页面。
  2. 因此,在您的交换机案例中检测将要显示哪种页面类型(您已经这样做了),那么如果当前页面是相同的类型,请不要再次导航,只需调用刷新方法:

    case MenuType.PageAType:
        if (_currentPage != null && _currentPage.GetType() == typeof(PageA))
        {
            var page = (PageA)_currentPage;
            page.Refresh();
        }
        else
        {
            _currentPage = new PageA();
            Pages.Add(id, new NavigationPage(_currentPage);
        }
        break;
    
    1. 或者,您可以使用Xamarin.Forms中的消息中心发送消息。
    2. 在您要刷新的页面中:

      MessagingCenter.Subscribe<MasterDetailPage>(this, "Refresh", (s) => {
          Refresh();
      });
      

      此行订阅来自MasterDetailPage类型的发件人的“刷新”消息。收到后,它会调用Refresh方法。

      然后在您的交换机箱中进行导航:

      MessagingCenter.Send<MasterDetailPage>(this, "Refresh");
      

      这将向此消息的所有订阅者发送消息。