【问题标题】:Navigate Content Pages MVVM xamarin导航内容页面 MVVM xamarin
【发布时间】:2019-07-24 09:07:42
【问题描述】:

有人可以教我如何在内容页面之间导航吗?我一直在阅读许多教程,但我无法实现它。我有这个小代码,我想在按下按钮时在页面之间切换。我使用 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# xamarin xamarin.forms


    【解决方案1】:

    只需调用

    Navigation.PushAsync(new ContentPage());
    

    INavigation.PushAsync Method

    将页面异步添加到导航堆栈的顶部。

    页面示例

    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());        
    }
    

    【讨论】:

    • 感谢您的回答!我之前尝试过,但出现以下错误:找不到类型或命名空间“PageNumberTwo”(您是否缺少或使用指令或程序集引用?)
    • @ArmandoAlvarez 是否存在 PageNumberTwo,如果存在,则需要在其命名空间中添加 using 指令
    • 我在其命名空间中使用了指令,我想这可能是 Visual Studio 的问题,我现在正在重新安装
    • @ArmandoAlvarez 虽然并非不可能,但它极不可能是视觉工作室问题
    • 我完成重新安装VS并给我同样的错误:(
    【解决方案2】:

    我发现了问题,我使用 Xamarin Live 进行测试,通过电缆连接没有任何错误。我不建议使用 Xamarin Live 不仅会带来这个问题,还会带来更多问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      • 2011-08-26
      • 2016-08-29
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      相关资源
      最近更新 更多