简体   繁体   中英

Navigate Content Pages MVVM xamarin

Can someone teach me how to navigate between pages of content? I have been reading many tutorials but I have not been able to achieve it. I have this small code I want to achieve that when pressing the buttons I change between the pages. I use MVVM model there is my MainViewModel

public class MainViewModel
{
    public Page1 PageNumberOne { get; set; }
    public Page2 PageNumberTwo { get; set; }

    public MainViewModel()
    {
        this.PageNumberOne = new Page1();
    }

}

there is my view model of Page1

public class Page1
{
    #region constructor
    public Page1()
    {
        GoPage2Command = new Command(async () => await GoPage2());
    }

    private async Task GoPage2()
    {
        await Application.Current.MainPage.DisplayAlert("", "Goin to page 2", "ok");
        //code to go PageNumberTwo here
    }
    #endregion

    #region Commands
    public Command GoPage2Command { get; set; }
    #endregion
}

GoPage2Command is binding to a button. there is my complete project up load to MF VS proyect

Simply call

Navigation.PushAsync(new ContentPage());

INavigation.PushAsync Method

Asynchronously adds a Page to the top of the navigation stack.

Example from the page

var newPage = new ContentPage ();
await Navigation.PushAsync (newPage);
Debug.WriteLine ("the new page is now showing");
var poppedPage = await Navigation.PopAsync ();
Debug.WriteLine ("the new page is dismissed");
Debug.WriteLine (Object.ReferenceEquals (newPage, poppedPage)); //prints "true"

Your Exmaple

private async Task GoPage2()
{
    await Application.Current.MainPage.DisplayAlert("", "Goin to page 2", "ok");
    //code to go PageNumberTwo here
    Navigation.PushAsync(new PageNumberTwo());        
}

I found the problem, I was using Xamarin Live to do the tests, connecting by cable does not give any error. I do not recommend using Xamarin Live not only gives that problem but many more

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM