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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1351546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:55:55  来源:igfitidea点击:

Passing parameters to a WPF Page via its Uri

wpfnavigationquery-stringnavigateuri

提问by Joe White

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

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

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/123or /Product.xaml?id=123, and have my Product.xaml page be able to see that it was called with a parameter of 123?

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

回答by Paul Stovell

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

你可以这样做。请参阅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.

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

<TextBlock>
    <Hyperlink NavigateUri="Page2.xaml?Message=Hello">Go to page 2</Hyperlink>
</TextBlock>

当页面加载时,它可以通过 提取参数 NavigationService.CurrentSource,它返回一个 Uri 对象。然后它可以检查 Uri 以分离这些值。但是,我强烈建议不要使用这种方法,除非在最可怕的情况下。

一个更好的方法是使用 NavigationService.Navigate 的重载,该重载接受一个对象作为参数。您可以自己初始化对象,例如:

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

这假设页面构造函数接收一个 Customer 对象作为参数。这允许您在页面之间传递更丰富的信息,而无需解析字符串。

回答by Jairo Andres Velasco Romero

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

Paul Stovell I think that using your suggestion will make your pages not garbage collected because the whole instance will remain on Journal.

Paul Stovell 我认为使用您的建议将使您的页面不会被垃圾收集,因为整个实例将保留在 Journal 上。

回答by ShadowKras

Another way is to create a public variable on the destiny page and use a get/set property to assign a value to it.

另一种方法是在命运页面上创建一个公共变量并使用 get/set 属性为其分配值。

On Page:

在页面上:

private Int32 pMyVar;

public Int32 MyVar
{
   get { return this.pMyVar; }
   set { this.pMyVar = value; }
}

When navigating to it:

导航到它时:

MyPagePath.PageName NewPage = new MyPagePath.PageName();
NewPage.MyVar = 10;

this.MainFrameName.NavigationService.Navigate(NewPage);

When NewPage is loaded, the integer MyVar will be equal to 10. MainFrameName is the frame you are using in case you are working with frame, but if not, the navigate command remains the same regardless. Its my opinion, but it seems easier to track it that way, and more user friendly to those who came from C# before WPF.

加载 NewPage 时,整数 MyVar 将等于 10。 MainFrameName 是您正在使用的框架,以防您使用框架,但如果不是,导航命令将保持不变。这是我的意见,但以这种方式跟踪它似乎更容易,并且对那些在 WPF 之前来自 C# 的人更友好。