新手

时间:2017-11-21 04:24:50

标签: c# xaml xamarin.forms master-detail

我已采用此Xamarin Forms演示中显示的方法。

https://github.com/xamarin/xamarin-forms-samples/tree/master/Navigation/MasterDetailPage/MasterDetailPageNavigation

但是当我创建一个新的MainPage时,Master和Detail都是null ...我希望它分别是我的MenuPage和TabPage。这是一个错误还是我做了一些根本错误的事情?

的MainPage:

<MasterDetailPage  xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:ctr="clr-namespace:MyApp.Core.Views;assembly=MyApp.Core"
         x:Class="SmartMiner.Core.Views.MainPage"
         >
<MasterDetailPage.Master>
    <ctr:MenuPage x:Name="MenuPage"/>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
    <ctr:TabPage x:Name="TabPage" />
</MasterDetailPage.Detail>

MenuPage(主)

<ContentPage 
xmlns="http://xamarin.com/schemas/2014/forms" 
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
Title="Menu"
x:Name="MenuPage"
x:Class="MyApp.Core.Views.MenuPage"
>
<ContentPage.Content>
</ContentPage.Content>

TabPage的(详情)

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" 
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
x:Class="MyApp.Core.Views.TabPage"
xmlns:tab="clr-namespace:SmartMiner.Core.Views;assembly=SmartMiner.Core"
Title="Tabs Page"
>
<tab:Tab1 Title="Tab One"/>
<tab:Tab2 Title="Tab Two" />
<tab:Tab3 Title="Tab Three" />

初​​始化

protected override void OnStart(){
        var mainPage = new MainPage();
        if (Device.RuntimePlatform == Device.iOS)
            MainPage = mainPage;
        else
            MainPage = new NavigationPage(mainPage); 
    }

检查var mainPage时的结果是Master和Detail属性为null并且MainPage的指定引发错误

&#34;在将主页和详细信息分配给MainPage&#34;

之前,必须先设置它

1 个答案:

答案 0 :(得分:1)

我可以发现一些问题。

需要在App构造函数中创建和分配主页面。也不要在NavigationPage中包装MainPage。您可以在详细信息页面中执行此操作。

您的App.xaml.cs应如下所示:

   public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

             MainPage = new 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
        }
    }

这是主详细信息页面(您的MainPage)

<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MasterDetailTest.MainPage"
             xmlns:ctr="clr-namespace:MasterDetailTest">
  <MasterDetailPage.Master>
    <ctr:MenuPage x:Name="MenuPage" />
  </MasterDetailPage.Master>
  <MasterDetailPage.Detail>
    <NavigationPage>
      <x:Arguments>
        <ctr:TabPage x:Name="TabPage" />
      </x:Arguments>
    </NavigationPage>
  </MasterDetailPage.Detail>
</MasterDetailPage>

菜单页

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MasterDetailTest.MenuPage"
             Title="Menu">
    <ContentPage.Content>
    </ContentPage.Content>
</ContentPage>

标签页

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:tab="clr-namespace:MasterDetailTest;assembly=MasterDetailTest"
             x:Class="MasterDetailTest.TabPage">
    <tab:Tab1Page Title="Tab 1" />
    <tab:Tab2Page Title="Tab 2" />
</TabbedPage>

其中一个标签页

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MasterDetailTest.Tab1Page">
    <ContentPage.Content>
        <StackLayout>
            <Label Text="Tab One"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>