在导航风格的 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 页面
发布评论