Switching ContentViews Xamarin.Forms

kerds78kerds78 Member ✭✭

I am trying to create a custom navigation Bar. I have set up my Navigation Bar and called it NavBar, and added a ContentView above it containing the Current Displayed page. I have created a public variable containing the ContentView like so: public ContentView CurrentPage = new Featured(); and I have a StackLayout set up in the Page like so:

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

I have a function that is called when a button on my NavBar is pressed to change the value of CurrentPage, set up like this:

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

But when the button is pressed, nothing happens. I think that the value is being changed, but not displayed on the screen. Is there any way to fix this? Thanks for any help in advance :)

Best Answer

  • kerds78kerds78 ✭✭
    Accepted Answer

    This answer was taken from reddit user brminnick1

    You need to replace CurrentPage inside of the StackLayout, which would
    look something like this:

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

Answers

  • kerds78kerds78 Member ✭✭
    Accepted Answer

    This answer was taken from reddit user brminnick1

    You need to replace CurrentPage inside of the StackLayout, which would
    look something like this:

    Device.BeginInvokeOnMainThread(() => stackLayout.Children[0] = new Buy());
    
Sign In or Register to comment.