Xamarin.Forms FlyoutPage 不适用于 Prism
Posted
技术标签:
【中文标题】Xamarin.Forms FlyoutPage 不适用于 Prism【英文标题】:Xamarin.Forms FlyoutPage is not working with Prism 【发布时间】:2021-07-01 07:20:30 【问题描述】:请帮帮我。为了让您理解我的问题,我在示例应用程序中复制了该场景
由于 Xamarin.Forms 在 5.0 版本中引入了 FlyoutPage 页面,导航在其中一个应用程序中不起作用。
我正在从app.xaml.cs
像这样await NavigationService.NavigateAsync("Page3");
打开主页
FlyoutPage:
<FlyoutPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:views="clr-namespace:PrismNavigation.Views;assembly=PrismNavigation"
xmlns:prism="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="PrismNavigation.Views.Page3"
Title="Master">
<FlyoutPage.Flyout>
<ContentPage Title="Menu">
<StackLayout>
<Button Text="Page2" HeightRequest="50" WidthRequest="100" Command="Binding NavigateCommand" CommandParameter="Page2"></Button>
</StackLayout>
</ContentPage>
</FlyoutPage.Flyout>
<FlyoutPage.Detail>
<NavigationPage>
<x:Arguments>
<views:Page1></views:Page1>
</x:Arguments>
</NavigationPage>
</FlyoutPage.Detail>
</FlyoutPage>
Page3ViewModel:
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PrismNavigation.ViewModels
public class Page3ViewModel : ViewModelBase
private readonly INavigationService navigationService;
public Page3ViewModel(
INavigationService navigationService)
: base(navigationService)
this.navigationService = navigationService;
this.NavigateCommand = new DelegateCommand<string>(this.NavigateCommandExecute);
public override void OnNavigatedTo(INavigationParameters parameters)
base.OnNavigatedTo(parameters);
private void NavigateCommandExecute(string obj)
this.navigationService.NavigateAsync($"NavigationPage/obj");
public DelegateCommand<string> NavigateCommand get; set;
应用:
using Prism;
using Prism.Ioc;
using PrismNavigation.ViewModels;
using PrismNavigation.Views;
using Xamarin.Essentials.Implementation;
using Xamarin.Essentials.Interfaces;
using Xamarin.Forms;
namespace PrismNavigation
public partial class App
public App(IPlatformInitializer initializer)
: base(initializer)
protected override async void OnInitialized()
InitializeComponent();
await NavigationService.NavigateAsync("Page3");
protected override void RegisterTypes(IContainerRegistry containerRegistry)
containerRegistry.RegisterSingleton<IAppInfo, AppInfoImplementation>();
containerRegistry.RegisterForNavigation<NavigationPage>();
containerRegistry.RegisterForNavigation<Page1>();
containerRegistry.RegisterForNavigation<Page2>();
containerRegistry.RegisterForNavigation<Page3, Page3ViewModel>();
当我试图从菜单导航到Page2
时,我不能这样做。当我单击按钮时,会执行导航命令,但不会发生导航。
【问题讨论】:
【参考方案1】:更新:
除了我在问题中修复的旧答案之外,根据this,Prism 8 似乎不支持 FlyoutPage。它计划用于 8.1。因此,您可以等待 8.1 或将其替换为 MasterDetailPage
。我用MasterDetailPage
尝试了你的例子,它对我来说很好:
<?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="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="PrismNavigation.Views.Page3">
<MasterDetailPage.Master>
<ContentPage Title="Menu">
<StackLayout Padding="20">
<Button Text="Page2" HeightRequest="50" WidthRequest="100" Command="Binding NavigateCommand" CommandParameter="Page2"></Button>
</StackLayout>
</ContentPage>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage>
<x:Arguments>
<ContentPage Title="This is Page1"></ContentPage>
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
</MasterDetailPage>
旧答案:
好像您没有将任何参数传递给您的NavigateCommand
。试试这个
<StackLayout>
<Button Text="Page2" HeightRequest="50" WidthRequest="100" Command="Binding NavigateCommand" CommandParameter="Page2"></Button>
</StackLayout>
【讨论】:
这是我的错,我在共享代码时忘记在此处添加该行。我的代码有参数但仍然无法正常工作 现在您添加了Page3
作为参数,而不是CommandParameter="Page3"
中的Page2
。从您的问题描述看来,您在导航到Page2
时遇到问题,因此您应该输入CommandParameter="Page2"
我已经更新了答案。试试MasterDetailPage
,它对我有用,或者等待 Prism v8.1。请让我知道这是否适合您。以上是关于Xamarin.Forms FlyoutPage 不适用于 Prism的主要内容,如果未能解决你的问题,请参考以下文章
Xamarin.Forms:如何在 Xamarin.Forms 跨平台项目中开发具有蓝牙连接的应用程序?
Xamarin.Forms:Forms.Context 已过时
Xamarin.Forms 和 Xamarin Native 有啥区别? [关闭]
如何使用 Xamarin.Forms.Maps(无 Xamarin.Forms.GoogleMaps)在地图中应用样式或更改颜色
Xamarin Forms Prism:prism ResourceDictionary 中已存在具有键“Xamarin.Forms.NavigationPage”的资源