【问题标题】:How to pass values (parameters) between XAML pages?如何在 XAML 页面之间传递值(参数)?
【发布时间】:2012-09-08 19:36:09
【问题描述】:

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

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

  1. 在 XAML 应用程序的页面之间导航时如何传递参数?
  2. 使用 Uri 导航和手动导航有什么区别?
  3. 使用 Uri 导航时如何传递对象(不仅仅是字符串)?

Uri 导航示例

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

手动导航示例

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

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

注意:Silverlight 和 Windows8 是有区别的

  • Windows Phone:页面导航到使用 Uri 和作为查询字符串或实例传递的数据
  • Windows 8:通过传递类型和参数作为对象导航到页面

【问题讨论】:

    标签: c# wpf xaml windows-8 windows-phone


    【解决方案1】:

    传递参数的方法

    1.使用查询字符串

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

    导航页面:

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

    目标页面:

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

    2。使用 NavigationEventArgs

    导航页面:

    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..";
        }
    }
    

    目标页面:

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

    3.使用手动导航

    导航页面:

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

    目标页面:

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

    Uri 和手动导航的区别

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

    传递复杂对象

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

    【讨论】:

    • 另外需要注意的是NavigationContext.QueryString.TryGetValue("parameter", out parameter)需要从下面的方法内部调用:protected override void OnNavigatedTo(NavigationEventArgs e)
    • typeof 操作符导航怎么样?你什么也没写
    【解决方案2】:

    对于那些因为没有 onNavigatedTo 函数来覆盖和使用框架进行导航而仍然存在问题的人,这是我使用的。

    在您从中导航的页面中: 假设这个页面被称为“StartPoint.xaml”

    NameOfFrame.Navigate(new System.Uri("Destination.xaml", UriKind.RelativeOrAbsolute), ValueToBePassed);
    
    
    • ValueToBePassed 在我的情况下是一个简单的字符串,我没有尝试过使用对象。 (通过简单的字符串我的意思是string code = "hello world";

    在 Destination.xaml 页面上: 创建页面加载事件。

    • 这可以通过转到页面的属性窗口>事件>加载>双击加载旁边的字段来自动生成函数并在代码隐藏中转到它来完成。

    Destinations.xaml > 加载

    private string x;
    
    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
       x = StartPoint.ValueToBePassed;
       //Call functions or do other stuff while im here
       //e.g. if (x != "") { please work you damn code }
       //     else { go to sleep and forget about the worries }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-10
      相关资源
      最近更新 更多