在页面之间传递数据 C# WPF

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

Passing data between pages C# WPF

c#wpfstringxamlbinding

提问by syn3rgy

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

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

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

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

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

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

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

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

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

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

然后在构造函数中订阅(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?

但是,当我运行该程序时,出现错误。有人可以指出我错过了什么吗?

回答by Peter Duniho

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.Navigate(object, object)重载对您很有用。

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

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

For example, in your first page:

例如,在您的第一页中:

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

then in your second page:

然后在你的第二页:

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

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

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

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

回答by ViVi

Instead of setting the textbox DataContext, set the entire page's datacontext to some class ie the ViewModel for page1. Implement the interface INotifyPropertyChangedand ensure that in the set of the string property, the NotifyPropertyChanged(“ElementName”)is raised.

不是设置文本框 DataContext,而是将整个页面的 datacontext 设置为某个类,即 page1 的 ViewModel。实现接口INotifyPropertyChanged并确保在字符串属性集中,引发 NotifyPropertyChanged(“ElementName”)

Now create a new view with a corresponding view model like this which should also implement the interface INotifyPropertyChanged. Create a textbox and bind it to a string property like in first page. Ensure TwoWaybinding for both properties to ensure both target and source is updated while data is changed.

现在创建一个具有相应视图模型的新视图,它也应该实现INotifyPropertyChanged接口。创建一个文本框并将其绑定到第一页中的字符串属性。确保两个属性的TwoWay绑定,以确保在数据更改时更新目标和源。

Create the instance of both viewModels in MainWindow. When user navigates to 2nd user control, set

在 MainWindow 中创建两个 viewModel 的实例。当用户导航到第二个用户控件时,设置

Page2ViewModel.TextBoxString = Page1ViewModel.TextBoxString;

Like this do vice versa while navigating from Page2 to Page1.

从 Page2 导航到 Page1 时,反之亦然。

Page1ViewModel.TextBoxString = Page2ViewModel.TextBoxString;

This way, both the textboxes will be updated during navigation. This is just an overall idea. You need to learn more about MVVM and WPF from some tutorials. Search in google.

这样,两个文本框都将在导航期间更新。这只是一个总体思路。您需要从一些教程中了解更多关于 MVVM 和 WPF 的知识。在谷歌搜索。