首页 > 解决方案 > 导航内容页面 MVVM xamarin

问题描述

有人可以教我如何在内容页面之间导航吗?我一直在阅读许多教程,但我无法实现它。我有这个小代码,我想在按下按钮时在页面之间切换。我使用 MVVM 模型有我的 MainViewModel

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

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

}

有我的 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 绑定到一个按钮。有我完整的项目上传到 MF VS proyect

标签: c#xamarinxamarin.forms

解决方案


只需调用

Navigation.PushAsync(new ContentPage());

INavigation.PushAsync 方法

将 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"

你的榜样

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

推荐阅读