切换ContentViews Xamarin.Forms

时间:2018-03-31 11:32:55

标签: c# xamarin xamarin.forms

我正在尝试创建自定义导航栏。我已经设置了导航栏并将其命名为NavBar,并在其上方添加了一个包含当前显示页面的ContentView。我创建了一个包含ContentView的公共变量,如下所示:public ContentView CurrentPage = new Featured();我在页面中设置了StackLayout,如下所示:

var stacklayout = new StackLayout { Spacing = 0 };
stacklayout.Children.Add(CurrentPage);
stacklayout.Children.Add(NavBar);
Content = stacklayout;

当按下NavBar上的按钮来更改CurrentPage的值时,我会调用一个函数,设置如下:

void NavToBuy(object sender, EventArgs e)
{
    CurrentPage = new Buy();
}

但按下按钮时,没有任何反应。我认为值正在改变,但不会显示在屏幕上。有没有什么办法解决这一问题?感谢您提前提供任何帮助:)

1 个答案:

答案 0 :(得分:3)

此答案取自reddit用户brminnick1

  

你需要替换StackLayout中的CurrentPage   看起来像这样:

Device.BeginInvokeOnMainThread(() => stackLayout.Children[0] = new Buy());