Xamarian.Form默认MasterDetail Project Application.Current.MainPage为null

时间:2019-03-09 13:40:57

标签: c# xamarin xamarin.android

我真的在尝试通过Xamarian.Form应用程序工作,我发现这很困难,因为开箱即用的模板似乎无法正常工作。

基本上,我正在菜单上导航至“浏览”或“关于”页面。 但是MainPage RootPage { get => Application.Current.MainPage as MainPage; }返回null。

我对此模板没有做任何更改,但是我确实了解C#,所以我大致理解了这个问题,但是我不知道为什么这肯定是从起始模板返回null的原因。

MenuPage.xaml.xs

using ProspectGator.Models;
using System;
using System.Collections.Generic;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace ProspectGator.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MenuPage : ContentPage
    {
        MainPage RootPage { get => Application.Current.MainPage as MainPage; }
        List<HomeMenuItem> menuItems;
        public MenuPage()
        {
            InitializeComponent();

        menuItems = new List<HomeMenuItem>
        {
            new HomeMenuItem {Id = MenuItemType.Browse, Title="Browse" },
            new HomeMenuItem {Id = MenuItemType.About, Title="About" }
        };

        ListViewMenu.ItemsSource = menuItems;

        ListViewMenu.SelectedItem = menuItems[0];
        ListViewMenu.ItemSelected += async (sender, e) =>
        {
            if (e.SelectedItem == null)
                return;

            var id = (int)((HomeMenuItem)e.SelectedItem).Id;
            await RootPage.NavigateFromMenu(id);
        };
    }
}
}

App.xaml.cs

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using ProspectGator.Views;

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace ProspectGator
{
    public partial class App : Application
    {

        public App()
        {
            InitializeComponent();
            MainPage = new NavigationPage( 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.xaml

0 个答案:

没有答案