通过 Uri 将参数传递给 WPF 页面

编程入门 行业动态 更新时间:2024-10-12 08:18:57
本文介绍了通过 Uri 将参数传递给 WPF 页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在导航风格的 WPF 应用程序(NavigationWindow,而不是 XBAP)的上下文中:

In the context of a navigation-style WPF application (NavigationWindow, not XBAP):

超链接的 NavigateUri 是否可以包含额外的参数,例如路径数据或查询字符串?例如,有什么方法可以将我的 NavigateUri 设置为 /Product.xaml/123 或 /Product.xaml?id=123,并让我的 Product.xaml 页面为能看到它是用 123 的参数调用的吗?

Is it possible for a Hyperlink's NavigateUri to contain extra parameters, like path data or a querystring? E.g., is there some way I could set my NavigateUri to /Product.xaml/123 or /Product.xaml?id=123, and have my Product.xaml page be able to see that it was called with a parameter of 123?

推荐答案

你可以这样做.请参阅 www.paulstovell/wpf-navigation:

You can do this. See www.paulstovell/wpf-navigation:

虽然不明显,但你可以将查询字符串数据传递给页面,以及从路径中提取它.例如,您的超链接可以传递一个值URI:

Although it's not obvious, you can pass query string data to a page, and extract it from the path. For example, your hyperlink could pass a value in the URI: <TextBlock> <Hyperlink NavigateUri="Page2.xaml?Message=Hello">Go to page 2</Hyperlink> </TextBlock>

页面加载完成后,通过提取参数NavigationService.CurrentSource,其中返回一个 Uri 对象.那么它可以检查 Uri 以拆开价值观.不过,我强烈推荐反对这种方法,除了在最可怕的情况.

When the page is loaded, it can extract the parameters via NavigationService.CurrentSource, which returns a Uri object. It can then examine the Uri to pull apart the values. However, I strongly recommend against this approach except in the most dire of circumstances.

更好的方法是使用过载为NavigationService.Navigate 需要参数的对象.你可以自己初始化对象,因为示例:

A much better approach involves using the overload for NavigationService.Navigate that takes an object for the parameter. You can initialize the object yourself, for example:

Customer selectedCustomer = (Customer)listBox.SelectedItem; this.NavigationService.Navigate(new CustomerDetailsPage(selectedCustomer));

这假定页面构造函数接收 Customer 对象作为范围.这可以让你通过页面之间的信息更丰富,并且无需解析字符串.

This assumes the page constructor receives a Customer object as a parameter. This allows you to pass much richer information between pages, and without having to parse strings.

更多推荐

通过 Uri 将参数传递给 WPF 页面

本文发布于:2023-11-24 02:22:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623621.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   页面   Uri   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!