gpt4 book ai didi

wpf - 通过 Uri 将参数传递到 WPF 页面

转载 作者:行者123 更新时间:2023-12-03 02:50:56 25 4
gpt4 key购买 nike

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

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

最佳答案

你可以做到这一点。请参阅http://www.paulstovell.com/wpf-navigation :

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>

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.

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));

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.

关于wpf - 通过 Uri 将参数传递到 WPF 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1351546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com