Issue
Because I'm new to Xamarin, I really have no idea how to switch between pages. so I refer to internet and found some solution. But there are some errors.Can anyone help to sort out this issue? Here's my code in app.cs file.I put comments to the codes that I'm not sure. When I click the btn1, that triggered an error called "pushAsync is not supported globally on iOS".
using System;
using Xamarin.Forms;
namespace mine
{
public class App : Application
{
public 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
}
}
}
Solution
Navigation is property of a VisualElement. All code for Button.Clicked is in constructor of App class(which is subcalass of Application), which has no "Navigation" property. In your case you can use MainPage.Navigation(because MainPage is a Page and Page is a VisualElement). But there is one more problem: your MainPage is not NavigationPage, so you cannot perform PushAsync(..). TO fix this problem you should just wrap ContentPage into Navigation Page like this:
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);
Btw, this arcticle will help u to understand what is going on.
EDIT :
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
}
}
}
I hope this works properly.
Answered By - Flame239
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.