因为我是Xamarin的新手,所以我真的不知道如何在页面之间切换。所以我指的是互联网并找到了一些解决方案。但是有一些错误。任何人都可以帮忙解决这个问题吗?这是我在app.cs文件中的代码。我对我不确定的代码添加了注释。当我点击btn1时,触发了一个名为“ios全局不支持pushAsync”的错误。
using System;
使用Xamarin.Forms;
命名空间我的 { 公共类App:应用程序 { 公共应用() {
// The root page of your application
Button btn1 = new Button();
Button btn2 = new Button();
Button btn3 = new Button();
btn1.Text= "Farmer";
btn2.Text= "Advisor";
btn3.Text= "Supplier";
MainPage = new ContentPage {
Content = new StackLayout {
VerticalOptions = LayoutOptions.Center,
Children = {
new Label {
XAlign = TextAlignment.Center,
Text = "Welcome to Rice Growing Advisor!"
//App.Navigation.PushAsync(new MyCustomContentPage());
//App.Navigation.PushAsync (new MainPage ());
}, btn1,btn2,btn3
}
}
};
//var np = new NavigationPage(new MainPage());
//MainPage=np;
//MainPage = new NavigationPage (MainPage);
//var rootPage = new NavigationPage(MainPage);
btn1.Clicked += async (sender, e) => {
//MainPage= new NavigationPage(new MainPage());
//MainPage = np;
await MainPage.Navigation.PushAsync(new NavigationPage());
MainPage.Navigation.RemovePage(MainPage);
};
}
protected override void OnStart ()
{
// Handle when your app starts
}
protected override void OnSleep ()
{
// Handle when your app sleeps
}
protected override void OnResume ()
{
// Handle when your app resumes
}
}
}
答案 0 :(得分:1)
导航是VisualElement的属性。 Button.Clicked的所有代码都在App类的构造函数中(它是Application的子类),它没有" Navigation"属性。 在您的情况下,您可以使用MainPage.Navigation(因为MainPage是一个页面,Page是一个VisualElement)。但还有一个问题:您的MainPage不是NavigationPage,因此您无法执行PushAsync(..)。要解决此问题,您应该将ContentPage包装到导航页面中,如下所示:
var page = new ContentPage {
Content = new StackLayout {
VerticalOptions = LayoutOptions.Center,
Children = {
new Label {
XAlign = TextAlignment.Center,
Text = "Welcome to Rice Growing Advisor!"
//App.Navigation.PushAsync(new MyCustomContentPage());
//App.Navigation.PushAsync (new MainPage ());
}, btn1,btn2,btn3
}
}
};
MainPage = new NavigationPage(page);
顺便说一下,this arcticle将帮助您了解正在发生的事情。
编辑:
using System;
using Xamarin.Forms;
namespace test
{
public class App : Application
{
public App ()
{
// The root page of your application
// set up properties via object initializer for clarity
Button btn1 = new Button {Text = "Farmer"};
Button btn2 = new Button {Text = "Advisor"};
Button btn3 = new Button {Text = "Supplier"};
var page = new ContentPage {
Content = new StackLayout {
VerticalOptions = LayoutOptions.Center,
Children = {
new Label {
// XAlign is obsolete, use HorizontalTextAlignment instead
HorizontalTextAlignment = TextAlignment.Center,
Text = "Welcome to Rice Growing Advisor!"
}, btn1,btn2,btn3
}
}
};
// wrap our page into NavigationPage
// if we dont do that, we'll get "PushAsync is not supported globally on iOS" exception
MainPage = new NavigationPage(page);
btn1.Clicked += async (sender, e) => {
var secondPage = new ContentPage
{
Content = new Label
{
Text = "Click \"BACK\" to navigate backward",
HorizontalTextAlignment = TextAlignment.Center,
VerticalTextAlignment = TextAlignment.Center
}
};
secondPage.ToolbarItems.Add(new ToolbarItem("BACK", null, async () => { await secondPage.Navigation.PopAsync();}));
await MainPage.Navigation.PushAsync(secondPage);
};
}
protected override void OnStart ()
{
// Handle when your app starts
}
protected override void OnSleep ()
{
// Handle when your app sleeps
}
protected override void OnResume ()
{
// Handle when your app resumes
}
}
}
我希望这能正常运作。
答案 1 :(得分:0)
getLogger
是Page的属性。你必须打电话给
Navigation