C# 导航内容页MVVM xamarin
有人能教我如何在内容页面之间导航吗?我已经阅读了很多教程,但我还没有做到这一点。我有一个小代码,我想在按下按钮时在页面之间切换。我使用MVVM模型,这是我的主视图模型C# 导航内容页MVVM xamarin,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,有人能教我如何在内容页面之间导航吗?我已经阅读了很多教程,但我还没有做到这一点。我有一个小代码,我想在按下按钮时在页面之间切换。我使用MVVM模型,这是我的主视图模型 public class MainViewModel { public Page1 PageNumberOne { get; set; } public Page2 PageNumberTwo { get; set; } public MainViewModel() { this.
public class MainViewModel
{
public Page1 PageNumberOne { get; set; }
public Page2 PageNumberTwo { get; set; }
public MainViewModel()
{
this.PageNumberOne = new 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只需调用
Navigation.PushAsync(new ContentPage());
异步将页面添加到导航堆栈的顶部
页面中的示例
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());
}
您的Exmaple
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());
}
我发现了问题,我正在使用Xamarin Live进行测试,通过电缆连接不会出现任何错误。我不推荐使用Xamarin Live,因为它不仅会带来这个问题,而且会带来更多问题谢谢您的回答!我以前尝试过,但我遇到了以下错误:找不到类型或命名空间“PageNumberTwo”(是否缺少或正在使用指令或程序集引用?@Armandalvarez PageNumberTwo是否存在,如果存在,则需要将using指令添加到其命名空间I将指令用于其命名空间,我想这可能是visual studio的问题,我正在重新安装now@ArmandoAlvarez这不太可能是visual studio的问题,但并非不可能。我完成了VS的重新安装,并给出了相同的错误:(