Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data between pages C# WPF

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)

    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)

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

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?

like image 591
syn3rgy Avatar asked Jul 08 '16 06:07

syn3rgy


People also ask

What are the two methods of passing data between pages?

Passing variables between pages using URL GET or POST method.

How do I move data from one page to another in WPF?

Data can be passed between pages in the NavigationWindow using the overloads of the 'Navigate()' method in the 'NavigationService' class. The following code snippet is used to pass data from one page to another. [C#] mypage newpage = new mypage(); window1.


1 Answers

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.

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.

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:

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.
    }
}
like image 75
Peter Duniho Avatar answered Oct 17 '22 06:10

Peter Duniho