我有一个使用Prism Template Pack(2.0.7)创建的Prism Xamarin.Forms应用程序。 所有套餐都是最新的:
我在https://github.com/dernippel/PrismNavApp
上托管了示例应用程序我有以下组件:
他们应该这样做:
仅在执行以下导航时才会起作用:
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资源库中找到任何已知错误。
答案 0 :(得分:0)
您无法在其他服务中使用NavigationService,尤其是在该服务是单身人士的情况下。 Xamarin.Forms中的导航特定于页面,并且仅在关联页面的上下文中起作用。相反,您的服务应该返回结果,您应该根据该结果从VM导航。不要尝试从服务中导航。