在 Windows Phone 的屏幕之间导航是否有类型安全的方式? [英] Is there a typesafe way of navigating between screens in Windows Phone?

查看:13
本文介绍了在 Windows Phone 的屏幕之间导航是否有类型安全的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在我的应用中的屏幕之间导航的方法.到目前为止,我所看到的基本上是将字符串 URI 传递给 NavigationService,并带有查询字符串参数.例如

I'm looking for a way to navigate between screens in my app. Basically what I've seen so far consists of passing a string URI to the NavigationService, complete with query string parameters., e.g.

NavigationService.Navigate(new Uri("/MainPage.xaml?selectedItem=" +bookName.Id, UriKind.Relative));

虽然最终我并不热衷于此,因为它需要魔法字符串,而且它们可能会导致问题.

I'm not really keen on this though ultimately because it requires magic strings, and they can lead to problems down the road.

理想情况下,我只创建一个我想要导航到的类的实例,将参数作为参数传递给构造函数.这可能吗?如果是,怎么办?

Ideally I'd just create an instance of the class I want to navigate to, passing the parameters as arguments to the constructor. Is this possible? If so, how?

推荐答案

虽然实际导航最终必须使用字符串,但您可以创建或使用类型安全的包装器.

While the actual navigation will have to use strings eventually, you can create or use a wrapper that is type safe.

我建议查看 Caliburn Micro,即使您仅将其用于类型安全导航.这是一个片段 来自在 WP8 中使用它的教程:

I would suggest looking at Caliburn Micro even if you only used it for the type safe navigation. Here is a snippet from a tutorial on using it in WP8:

工具包附带的 NavigationService 支持视图模型优先方法:而不是声明我们希望将用户带到哪个页面的 URL(这是标准方法),我们声明哪个是我们要显示的 ViewModel.该服务将负责创建正确的 URL 并显示与视图模型关联的视图.

The NavigationService that comes with the toolkit supports a view model first approach: instead of declaring which is the URL of the page where we want to take the user (that is the standard approach), we declare which is the ViewModel we want to display. The service will take care of creating the correct URL and display the view that is associated with the view model.

或者,您可以查看 Windows Phone MVC,它也有一些类型安全的导航.您甚至可以将导航代码提取出来供您自己使用,因为它已根据 MS-PL<获得许可/a>.

Alternatively you could look at Windows Phone MVC which also has some type safe navigation. You might even just be able to pull the navigation code out to use on your own since it's licensed under MS-PL.

这篇关于在 Windows Phone 的屏幕之间导航是否有类型安全的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆