如何从CarouselPage导航到MasterDetailPage

时间:2015-06-30 04:54:22

标签: c# xamarin xamarin.forms

我必须从CarouselPage导航到MasterDetailPage。我创建了两个不同的我的第一页是旋转木马页面点击curousel页面中的任何按钮我应该导航到masterdeatilpage。

请帮我解释如何编写代码,我的母版页不同,详细页面不同,都写在内容页面上。

代码:

app.cs

public static MasterDetailPage MasterDetailPage;
public App()
{
    var isLoggedIn = App.Current.Properties.ContainsKey("IsLoggedIn")? (bool)Properties["IsLoggedIn"] : false;
        // The root page of your application
        if (isLoggedIn)
        {
            MasterDetailPage = new Xamarin.Forms.MasterDetailPage
            {
                Master = new Menu(),
                Detail = new NavigationPage(new detail())
                {
                    Tint = Color.FromHex("313FA0")
                }
            };

            MasterDetailPage.MasterBehavior = MasterBehavior.Popover;

            MainPage = MasterDetailPage;
        }
        else
        {
           MainPage = new NavigationPage(new carousel());
        }
}

carousel.xaml.cs

<CarouselPage>
 <ContentPage>
   <StackLayout>
     <Button Text="click" Clicked="funca()"/>
   </StackLayout>
  </ContentPage>
</CarouselPage>

carousel.cs

void funca(object sender, EventArgs e){
    App.MasterDetailPage.Master = new Menu();
    App.MasterDetailPage.Detail = new NavigationPage(new detail());
    App.MasterDetailPage.IsPresented = false;  
}

单击按钮会显示错误

System.Exception: Object reference not set to an instance of an object

请帮帮我

2 个答案:

答案 0 :(得分:1)

请查看我的样本仓库here。我正在使用基于MessagingCenter的{​​{3}}解决方案我相信它也适用于您的方案。请务必在MasterDetailPage中添加与there类似的内容。

答案 1 :(得分:0)

我在@Tomasz Kowalczyk的帮助下找到了给定问题的答案谢谢老兄

我创建了一个类MasterPage.cs

public class MasterPage : MasterDetailPage
{
    public MasterPage()
    {
        Master = new Menu();
        Detail = new NavigationPage(new detail());
    }
}

app.cs

public App()
{
  var isLoggedIn = App.Current.Properties.ContainsKey("IsLoggedIn")? (bool)Properties["IsLoggedIn"] : false;
    // The root page of your application
    if (isLoggedIn)
    {
        MainPage = new MasterPage();
    }
    else
    {
       MainPage = new NavigationPage(new carousel());
    }
}

carousel.cs

void funca(object sender, EventArgs e){
   MasterPage mp = new Menu();
   mp.Detail = new NavigationPage(new Detail());
   App.Current.MainPage = mp;
}