【问题标题】:Navigating in PCL using Prism使用 Prism 在 PCL 中导航
【发布时间】:2016-09-03 13:04:55
【问题描述】:

我正在使用 prism 使用 PCL 构建 UWP 应用程序。

PCL 包含 ViewModels

UWP 包含视图

我的问题是我无法在 PCL 中使用 INavigationService,因此我无法真正导航到其他页面。

这是我的代码:

MainPage.xaml(在 UWP 项目中):

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

        <SplitView x:Name="RootSplitView"
                   DisplayMode="Overlay"
                   IsTabStop="False">
            <SplitView.Pane>
                <StackPanel Margin="0,50,0,0">
                    <Button Content="Second" Command="{x:Bind ViewModel.SecondPageCommand}"  />
                    <Button Content="Third" />
                </StackPanel>
            </SplitView.Pane>

            <!-- OnNavigatingToPage we synchronize the selected item in the nav menu with the current page.
                 OnNavigatedToPage we move keyboard focus to the first item on the page after it's loaded and update the Back button. -->
            <Frame x:Name="FrameContent" />
        </SplitView>

        <ToggleButton x:Name="TogglePaneButton"
                      TabIndex="1"                      
                      IsChecked="{Binding IsPaneOpen, ElementName=RootSplitView, Mode=TwoWay}"
                      ToolTipService.ToolTip="Menu"
                      Style="{StaticResource SplitViewTogglePaneButtonStyle}"
                      />
    </Grid>

ViewModel(在 PCL 中):

 public class NavigationRootViewModel : BindableBase
    {
        public ICommand SecondPageCommand { get; set; }
        public NavigationRootViewModel()
        {
            SecondPageCommand = DelegateCommand.FromAsyncHandler(ExecuteMethod);
        }

        private Task ExecuteMethod()
        {
            return Task.FromResult(0);
        }
    }

我希望在构造函数中注入 INavigationService,但它不是 Prism.Core dll 的一部分。

那么正确的做法是什么?甚至可以使用 Prism 在 PCL 中导航吗?在 MvvmCross 中是...

【问题讨论】:

    标签: c# navigation uwp prism printer-control-language


    【解决方案1】:

    在 Prism 中导航不是以跨平台方式构建的,因此如果没有您自己的代码,它将无法工作。

    要解决这个问题,请在 PCL 中创建一个IAppNavigationService 接口:

    public interface IAppNavigationService
    {
        bool Navigate(string pageToken, object parameter);
        void GoBack();
        bool CanGoBack();
    }
    

    在 Windows 项目中实现它,基本上只是作为 INavigationService 的包装器,您也可以注入它:

    public class AppNavigationService : IAppNavigationService
    {
       private readonly INavigationService _nav;
    
       public AppNavigationService( INavigationService navigationService )
       {
          _nav = navigationService;
       } 
    
       public bool Navigate(string pageToken, object parameter) => 
          _nav.Navigate( pageToken, parameter );
    
       public void GoBack()
       {
          _nav.GoBack();
       }
    
       public bool CanGoBack() => _nav.CanGoBack();
    }
    

    不要忘记在App.xaml.cs的依赖注入容器中注册AppNavigationService类。

    现在,您可以在 PCL ViewModels 中注入并使用 IAppNavigationService 进行导航。

    【讨论】:

    • 是的,这是一个很好的答案。装饰器模式将完成这项工作。奇怪的是,棱镜为什么不支持开箱即用的 PCL 导航。谢谢
    • 这可能是因为不同平台上使用的导航范式之间存在差异,因此拥有它可以让您准确地找出您需要的内容。另一方面,MvvmCross 有一个很好的解决方案,它也是可扩展的。
    【解决方案2】:

    我在我们的项目中使用棱镜导航如下。

    var regionManager = container.Resolve<IRegionManager>();
    if (screenName == SpEnums.ViewName.LuminationView.ToString())
    {
         regionManager.RequestNavigate(Enums.Regions.MainRegion.ToString(),
         SpEnums.ViewName.LuminationView.ToString());
    }
    else if()...
    

    屏幕名称是导航视图模型中的一个属性。

    希望这会有所帮助。

    【讨论】:

    • 我知道 WPF 应用程序的区域经理。它不是 prism 核心的一部分,因此不能在 PCL 中使用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    相关资源
    最近更新 更多