在页面之间传递数据 C# WPF [英] Passing data between pages C# WPF

查看:52
本文介绍了在页面之间传递数据 C# WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在页面之间传递一个简单的字符串,但我不知道如何.

I am trying to pass a simple string between pages but I don't know how to.

我创建了一个测试应用程序,在其中单击一个按钮,a"的值将传递到下一页 (Page1.xaml.cs)

I created a test app where I click a button, the the value of "a" will pass on the next page (Page1.xaml.cs)

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        string a = "hello";

        Page2 p2 = new Page2();
        NavigationService.Navigate(p2, a);
    }

现在,我想从 Page1 (Page2.xaml.cs) 中提取数据

Now, I want to extract the data from Page1 (Page2.xaml.cs)

    private void NavigationService_LoadCompleted(object sender, NavigationEventArgs e)
    {
        string str = (string)e.ExtraData;
    }

然后在构造函数中订阅(Page2.xaml.cs)

And then subscribing in the constructor (Page2.xaml.cs)

    public Page2()
    {
        InitializeComponent();
        NavigationService.LoadCompleted += NavigationService_LoadCompleted;
    }

但是,当我运行程序时出现错误.有人能指出我遗漏了什么吗?

However when I ran the program I get an error. Can someone point out what am I missing?

推荐答案

没有一个好的最小、完整和可验证的代码示例,不可能确定知道解决您的问题所需的一切.但是,如果您要问的只是在导航时如何允许数据从一页传递到下一页,那么在我看来 NavigationService.Navigate(object, object) 重载对您很有用.

Without a good Minimal, Complete, and Verifiable code example, it's impossible to know for sure everything that would be needed to address your question. However, if all you're asking is how to allow data to pass from one page to the next when navigating, it seems to me that the NavigationService.Navigate(object, object) overload would be useful for you.

第二个参数是你要传递的数据.目标页面可以处理NavigationService.LoadCompleted 事件(或任何其他您喜欢的适当事件),其中传递给 Navigate() 方法的对象值可以是通过 检索NavigationEventArgs.ExtraData 属性.

The second parameter is the data you want to pass. The target page can handle the NavigationService.LoadCompleted event (or any other appropriate one you prefer), where the object value that was passed to the Navigate() method can be retrieved via the NavigationEventArgs.ExtraData property.

例如,在您的第一页:

private void button_Click(object sender, RoutedEventArgs e)
{
    Page2 p2 = new Page2();
    NavigationService.Navigate(p2, v.str);
}

然后在你的第二页:

private void NavigationService_LoadCompleted(object sender, NavigationEventArgs e)
{
    string str = (string)e.ExtraData;

    // do whatever with str, like assign to a view model field, etc.
}

当然,您将订阅事件处理程序,例如在页面的构造函数或 XAML 中.例如:

Of course, you'll subscribe the event handler, e.g. in your page's constructor or in XAML. For example:

public partial class Page2 : Page
{
    public Page2()
    {
        InitializeComponent();

        NavigationService.LoadCompleted += NavigationService_LoadCompleted;
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        NavigationService.GoBack();
    }

    private void NavigationService_LoadCompleted(object sender, NavigationEventArgs e)
    {
        string str = (string)e.ExtraData;

        // do whatever with str, like assign to a view model field, etc.
    }
}

这篇关于在页面之间传递数据 C# WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆