问题描述
请帮帮我.为了让您理解我的问题,我在示例应用程序中复制了该场景
Please help me out. In order for you to understand my problem, I have replicated the scenario in sample application
由于 Xamarin.Forms 在 5.0 版本中引入了 FlyoutPage 页面,导航在其中一个应用程序中不起作用.我从 app.xaml.cs
像这样打开主页 await NavigationService.NavigateAsync("Page3");
As Xamarin.Forms introduced FlyoutPage page in 5.0 version, navigation is not working in one of the applications.I am opening main page from app.xaml.cs
like this await NavigationService.NavigateAsync("Page3");
弹出页面:
<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:
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
时,我不能这样做.当我点击按钮时,导航命令被执行,但导航没有发生.
When I am trying to navigate to Page2
from menu, i can not do that. When I click the button, navigate command is executed, but navigation is not happening.
推荐答案
更新:除了我在问题中修复的旧答案之外,根据 这个.计划用于 8.1.因此,您可以等待 8.1 或将其替换为 MasterDetailPage
.我用 MasterDetailPage
尝试了你的例子,它对我来说很好:
Update:In addition to my old answer, which you have fixed in the question, it seems that the FlyoutPage is not supported by Prism 8 according to this. It is planned for 8.1. So, you can either wait 8.1 or replace it with MasterDetailPage
. I tried your example with MasterDetailPage
and it worked fine for me:
<?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
传递任何参数.试试这个
Old answer:Seems like you didn't pass any parameter to your NavigateCommand
. Try this
<StackLayout>
<Button Text="Page2" HeightRequest="50" WidthRequest="100" Command="{Binding NavigateCommand}" CommandParameter="Page2"></Button>
</StackLayout>
这篇关于Xamarin.Forms FlyoutPage 不适用于 Prism的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!