C# 如何在 XAML 页面之间传递值(参数)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12444816/
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-08-09 23:18:11  来源:igfitidea点击:

How to pass values (parameters) between XAML pages?

c#wpfxamlwindows-8windows-phone

提问by Daniel Little

Similar questions have been asked before but this question strives to explore more options and the ability to pass complex objects.

之前也有人问过类似的问题,但这个问题力求探索更多选项和传递复杂对象的能力。

The question is how to pass parameters but it really needs to be broken up into three parts..

问题是如何传递参数,但它确实需要分解为三个部分..

  1. When navigating between pages in an XAML application how do you pass parameters?
  2. What is the difference between using the Uri navigation and manual navigation?
  3. How can objects (not just strings) be passed when using Uri navigation?
  1. 在 XAML 应用程序中的页面之间导航时,如何传递参数?
  2. 使用 Uri 导航和手动导航有什么区别?
  3. 使用 Uri 导航时如何传递对象(不仅仅是字符串)?

Example of Uri navigation

Uri 导航示例

page.NavigationService.Navigate(new Uri("/Views/Page.xaml", UriKind.Relative));

Example of manual navigation

手动导航示例

page.NavigationService.Navigate(new Page());

The answer to this question applies to WP7, silverlight, WPF and Windows 8.

这个问题的答案适用于 WP7、silverlight、WPF 和 Windows 8。

Note: There is a difference between Silverlight and Windows8

注意:Silverlight 和 Windows8 有区别

  • Windows Phone: pages are navigated to using a Uri and data passed as a query string or an instance
  • Windows 8: pages are navigated to by passing the type, and parameters as objects
  • Windows Phone:使用 Uri 和作为查询字符串或实例传递的数据导航到页面
  • Windows 8:通过将类型和参数作为对象传递来导航到页面

采纳答案by Daniel Little

Methods to pass parameters

传递参数的方法

1. Using the query string

1. 使用查询字符串

You can pass parameters through the query string, using this method means have to convert your data to strings and url encode them. You should only use this to pass simple data.

您可以通过查询字符串传递参数,使用此方法意味着必须将您的数据转换为字符串并对其进行 url 编码。您应该只使用它来传递简单的数据。

Navigating page:

导航页面:

page.NavigationService.Navigate(new Uri("/Views/Page.xaml?parameter=test", UriKind.Relative));

Destination page:

目的地页面:

string parameter = string.Empty;
if (NavigationContext.QueryString.TryGetValue("parameter", out parameter)) {
    this.label.Text = parameter;
}

2. Using NavigationEventArgs

2. 使用 NavigationEventArgs

Navigating page:

导航页面:

page.NavigationService.Navigate(new Uri("/Views/Page.xaml?parameter=test", UriKind.Relative));

// and ..

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    // NavigationEventArgs returns destination page
    Page destinationPage = e.Content as Page;
    if (destinationPage != null) {

        // Change property of destination page
        destinationPage.PublicProperty = "String or object..";
    }
}

Destination page:

目的地页面:

// Just use the value of "PublicProperty"..

3. Using Manual navigation

3. 使用手动导航

Navigating page:

导航页面:

page.NavigationService.Navigate(new Page("passing a string to the constructor"));

Destination page:

目的地页面:

public Page(string value) {
    // Use the value in the constructor...
}

Difference between Uri and manual navigation

Uri 和手动导航的区别

I think the main difference here is the application life cycle. Pages created manually are kept in memory for navigation reasons. Read more about it here.

我认为这里的主要区别在于应用程序生命周期。出于导航原因,手动创建的页面保存在内存中。在此处阅读更多相关信息。

Passing complex objects

传递复杂对象

You can use method one or two to pass complex objects (recommended). You can also add custom properties to the Applicationclass or store data in Application.Current.Properties.

您可以使用方法一或方法二来传递复杂对象(推荐)。您还可以向Application类添加自定义属性或将数据存储在Application.Current.Properties.