Xamarin MasterDetailPage和 NavigationPage

学习Xamarin的Pages,想使用MasterDetailPage作为主页,随手写了一段代码。

public class MainPage:MasterDetailPage
{
    public MainPage()
    {
        this.Master=new ContentPage
        {
            Title="Master Page",
            Content = new StackLayout{ ... }
        };

        this.Detail=new ContentPage{ ... };
    }
}

//App.cs中设置主页
MianPage=new NavigationPage(new MainPage());

在Xamarin Forms UWP平台上运行,结果发现Master/Detail之间不能切换,设置MasterDetailPage属性 IsPresented=false之后,就回不到MasterPage页面了,后来去看了一下官方的Demo发现对MasterDetailPage使用导航页面(NavigationPage)的方法不对。正确代码如下:

public class MainPage:MasterDetailPage
{
    public MainPage()
    {
        this.Master=new ContentPage
        {
            Title="Master Page",
            Content = new StackLayout{}
        };

       this.Detail=new NavigationPage(new ContentPage{ ... });
    }
}

//App.cs中设置主页
MianPage=new MainPage();

不要直接对MasterDetailPage使用 NavigationPage,而是对DetailPage使用。

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s