简体   繁体   中英

How to open new TabbedPage from ContentPage?

I am trying to open (using any method) the TabbedPage from ContentPage.

My main App code:

public class App : Application
{
    public App ()
    {
        MainPage = new ConnectPage ();
    }
}

My ConnectPage uses XAML, code:

.cs file:

public partial class ConnectPage : ContentPage
    {
        public ConnectPage ()
        {
            InitializeComponent ();
        }

        void connectDevice(object sender, EventArgs args){
            connect_btn.Text = "Connecting...";
            connect_btn.IsEnabled = false;
            var mainapp_page = new MainApp ();
            Navigation.PushAsync (mainapp_page);
        }
    }

XAML file:

<Button x:Name="connect_btn" Text="Connect Now" Clicked="connectDevice"></Button>

Above method throws error:

PushAsync is not supported globally on iOS, please use a NavigationPage

My MainApp.cs (which contain tabs):

public class MainApp : ContentPage
{
    public MainApp ()
    {
        var tabs = new TabbedPage ();
        tabs.Children.Add (new Tab1Page{Title="Tab1" });
        tabs.Children.Add (new Tab2Page{Title="Tab2" });
        tabs.Children.Add (new Tab3Page{Title="Tab3" });
    }
}

You can either update your'r app's MainPage property and set that to the page you want to display, use stack navigation or present a page modally.

Setting a new main page will provide no way for the user to go back:

App.Current.MainPage = new SomeOtherPage ();

If you want to use stack navigation, you will have to wrap your initial page into a NavigationPage :

public partial class App : Application
{
    public App ()
    {
        InitializeComponent ();
        this.MainPage = new NavigationPage (new FirstPage ());
    }
}

Than you can use Navigation.PushAsync() .

If you want to present a page modally, so it is shown on top of your current page and can be dismissed, use:

Navgiation.PushModalAsync(new Page());

However, this will still require to wrap your current page into a NavigationPage .

There are other ways too, like CarouselPage or MasterDetailPage . I recommend you look at the documentation for all of your options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM