服务中的棱镜导航仅在不导航回

时间:2017-11-28 15:15:30

标签: xamarin.forms prism

我有一个使用Prism Template Pack(2.0.7)创建的Prism Xamarin.Forms应用程序。 所有套餐都是最新的:

  • Xamarin.Forms v2.5.0.91635
  • Xamarin.Android。* v26.1.0.1
  • Prism.DryIoc.Forms v7.0.0.168-pre(由于启动时Android Renderer中的NullReferenceException,无法更新到v7.0.0.269-pre,但是我的主应用程序使用的是269-pre而没有这个问题)

我在https://github.com/dernippel/PrismNavApp

上托管了示例应用程序

我有以下组件:

  • 模块"附件" (棱镜模块)
  • 模块"查看器" (棱镜模块)
  • 服务"附件服务"使用Interface作为Singleton在Container注册

他们应该这样做:

  1. AttachmentsPage(来自AttachmentsModule)列出了一些对象
  2. 选择一个附件
  3. AttachmentPageViewModel调用" OpenAttachment" - 附件服务的方法
  4. Method按类型确定正确的ViewerPage,并使用Prism-NavigationService直接导航到Page(在示例中,这是" ImageViewerPage" ViewerModule)
  5. 仅在执行以下导航时才会起作用:

    MainPage - > AttachmentsPage - > ViewerPage - > (后箭头)AttachmentsPage - > ViewerPage(等等)

    但是如果你导航回MainPage导航到ViewerPage就不再工作了:

    MainPage - > AttachmentsPage - > ViewerPage - > (后箭头)AttachmentsPage - > (后箭头)MainPage - > AttachmentsPage - > (当点击按钮导航到ViewerPage时,没有任何反应)

    AttachmentsService通过Constructor注入获取NavigationService并以这种方式导航:

    public AttachmentService(INavigationService navigationService)
        {
            this.navigationService = navigationService;
        }
    
        public async void OpenAttachmentWithViewer(object attachment)
        {
            // ToDo: handle parameter proberbly
            var attachmentType = "image";
    
            // select correct viewer
            if (attachmentType == "image")
            {
                // navigate to image viewer
                var navParams = new NavigationParameters();
                navParams.Add("object",attachment);
                var navTask = this.navigationService.NavigateAsync(
                    "ImageViewerPage",
                    navParams,
                    useModalNavigation: false);
                await navTask;
    
                var result = navTask.Status;
                Debug.WriteLine($"Navigation State is {result}");
            }
        }
    

    我尝试检查导航任务结果状态,它总是" RanToCompletion"。

    修改AttachmentsPageViewModel以直接使用Prism NavigationService导航而不是使用服务不会导致此行为:

    private void OnOpenAttachment()
        {
            // ToDo: get the selected attachment
            object selectedAttachment = null;
    
            // navigating inside a service -- not working when navigating back to MainPage
            //this.attachmentService.OpenAttachmentWithViewer(selectedAttachment);
    
            // navigation from view model -- working
            var navParams = new NavigationParameters();
            navParams.Add("object", selectedAttachment);
            this.navigationService.NavigateAsync("ImageViewerPage", navParams, useModalNavigation: false);
        }
    

    提示:我使用基于PCL的主应用程序切换到基于.NETStandard的新解决方案,并且已经使用Prism v6.3.0.1成功运行了类似的功能。自端口以来,此功能甚至无法导航一次。

    其实我不知道如何解决这个问题。

    是否可以查看Prism NavigationService以确定导航未发生的原因?

    我还没有在Prism资源库中找到任何已知错误。

1 个答案:

答案 0 :(得分:0)

您无法在其他服务中使用NavigationService,尤其是在该服务是单身人士的情况下。 Xamarin.Forms中的导航特定于页面,并且仅在关联页面的上下文中起作用。相反,您的服务应该返回结果,您应该根据该结果从VM导航。不要尝试从服务中导航。

相关问题