无法让这个MasterDetail与Prism和listview一起使用

时间:2017-08-07 07:49:16

标签: xamarin prism

以下是我的代码无效你能发现什么是错的吗?

我正在尝试将其转换为棱镜 https://github.com/xamarin/xamarin-forms-samples/tree/master/Navigation/MasterDetailPage

我一直在

" System.InvalidOperationException: Android上不支持全局支持PushAsync,请使用NavigationPage。"

我正在使用导航页面(我想我这样做)

有人可以查看代码.Below是所有代码并告诉我我做错了什么?

非常感谢!

app.cs

public partial class App : PrismApplication
        {
            public App(IPlatformInitializer initializer = null) : base(initializer) { }

            protected override void OnInitialized()
            {
                   InitializeComponent();

                    NavigationService.NavigateAsync("MainPage/Navigation/ContactsPage");                        
            }

            protected override void RegisterTypes()
            {
                //Container.RegisterTypeForNavigation<NavigationPage>("Navigation");
                Container.RegisterTypeForNavigation<MainNavigationPage>("Navigation");

                Container.RegisterTypeForNavigation<MainPage,MainPageViewModel>();

                Container.RegisterTypeForNavigation<ContactsPage,ContactsPageViewModel>();
                Container.RegisterTypeForNavigation<MasterPage,MasterPageViewModel>();
                Container.RegisterTypeForNavigation<ReminderPage,ReminderPageViewModel>();
                Container.RegisterTypeForNavigation<TodoListPage,TodoListPageViewModel>();

            }
        } 

MainNavigationPage.Xaml

<?xml version="1.0" encoding="utf-8" ?>
    <NavigationPage xmlns="http://xamarin.com/schemas/2014/forms"
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                    xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
                    prism:ViewModelLocator.AutowireViewModel="True"
                    x:Class="MasterDetailReferenceApp.Views.MainNavigationPage">

    </NavigationPage>

MainPage.xaml中

<?xml version="1.0" encoding="utf-8" ?>
        <MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
                     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                     xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
                     xmlns:views="clr-namespace:MasterDetailReferenceApp.Views;assembly=MasterDetailReferenceApp"
                     prism:ViewModelLocator.AutowireViewModel="True"
                     x:Class="MasterDetailReferenceApp.Views.MainPage"
                     Title="MainPage">
           <MasterDetailPage.Master>
    <views:MasterPage x:Name="masterPage" />
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
    <NavigationPage>
        <x:Arguments>
            <views:ContactsPage />
        </x:Arguments>
    </NavigationPage>
</MasterDetailPage.Detail>

MainPage.cs

public partial class MainPage : MasterDetailPage,IMasterDetailPageOptions
        {
            public MainPage()
            {
                InitializeComponent();
            }

            public bool IsPresentedAfterNavigation => Device.Idiom != TargetIdiom.Phone;
        }

<?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
                 prism:ViewModelLocator.AutowireViewModel="True"
                 x:Class="MasterDetailReferenceApp.Views.MasterPage"             
                 Icon="hamburger.png"
                 Title="Personal Organiser">
        <ContentPage.Content>
            <StackLayout VerticalOptions="FillAndExpand">
                <ListView x:Name="listView" 
                          RowHeight="60"
                          SeparatorVisibility="None"
                          BackgroundColor="White"  
                          HasUnevenRows="true"
                          ItemSelected="OnMenuItemSelected">
                          >
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ImageCell Text="{Binding Title}" ImageSource="{Binding IconSource}" />
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>

MasterPage.cs

 public partial class MasterPage : ContentPage
    {
        public ListView ListView => listView;

        public MasterPage()
        {
            InitializeComponent();

            var masterPageItems = new List<MasterPageItem>();
            masterPageItems.Add(new MasterPageItem
            {
                Title = "Contacts",
                //Uri = "ContactsPage",
                Uri = "Navigation/ContactsPage",
                IconSource = "contacts.png",
                TargetType = typeof(ContactsPage)
            });
            masterPageItems.Add(new MasterPageItem
            {
                Title = "TodoList",
                Uri = "Navigation/TodoListPage",
                //Uri = "TodoListPage",
                IconSource = "todo.png",
                TargetType = typeof(TodoListPage)
            });
            masterPageItems.Add(new MasterPageItem
            {
                Title = "Reminders",
                Uri = "Navigation/RemindersPage",
                IconSource = "reminders.png",
                TargetType = typeof(ReminderPage)
            });


            listView.ItemsSource = masterPageItems;
        }

        private void OnMenuItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var item = (MasterPageItem)e.SelectedItem;

            ((MasterPageViewModel)BindingContext).NavigateCommand.Execute(item.Uri);

        }
    }

MasterPageItem

public class MasterPageItem
            {
                public string Title { get; set; }
                public string Uri { get; set; }
                public string IconSource { get; set; }
            }

1 个答案:

答案 0 :(得分:0)

您需要将一些NavigationPage设置为MasterDetailPage的Detail。像这样:

    <MasterDetailPage.Detail>
        <NavigationPage BarTextColor="White">
            <x:Arguments>
                <local:StatisticsPage />
            </x:Arguments>
        </NavigationPage>
    </MasterDetailPage.Detail>

在此示例中,导航页面中的第一页将是StatisticsPage 然后,您可以从StatisticsPageViewModel执行

await _navigationService.NavigateAsync(new Uri(navParam, UriKind.Relative), useModalNavigation: isModal);

其中navParam为nameof(SettingsPage)。这会将NavigationPage内容切换到SettingsPage。

如果您想远离MasterDetailPage,请执行

await NavigationService.NavigateAsync(nameof(OtherNonMasterDetailPage));
来自MasterDetailPageViewModel的

(不是来自NavigationPage内容的视图模型)