0

I'm working on a project to develop a UWP app.

I'm getting the above mentioned error when I click a button intended to navigate to another frame.

Here's the code of the first frame:

private void Search_Click(object sender, RoutedEventArgs e)
    {
        Submit();
    }
    void Submit()
    {
            DateTime? pickupdate;
            pickupdate = PickupDate.Date.DateTime;
            DateTime? retdate;
            retdate = ReturnDate.Date.DateTime;

            Reservation res = new Reservation(pickupdate.Value.ToString("dd-MM-yyyy"), retdate.Value.ToString("dd-MM-yyyy"));
            Frame.Navigate(typeof(Reservation));
    }

And the code for the second frame:

public Reservation(string pickup, string _return)
    {
        InitializeComponent();
        PickupDateDisplay.Text = pickup;
        ReturnDateDisplay.Text = _return;
    }

UPDATE:

Ok so I overloaded the Reservation() constructor and now the code for the second frame looks like this:

    public Reservation()
    {
        InitializeComponent();
    }

    public Reservation(string pickupdate,string retdate)
    {
        InitializeComponent();
        PickupDateDisplay.Text = pickupdate;
        ReturnDateDisplay.Text = retdate;
    }

Now I don't get any errors on runtime but the PickupDateDisplay.Text and ReturnDateDisplay.Text does not change

Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
Jestin
  • 69
  • 1
  • 10
  • 1
    Read the answer, do what it says, if you can't fix, come back [edit] and add details with what you found, what you tried, and what happened. –  Feb 22 '17 at 19:25
  • This is happening because when navigating using `Frame.Navigate` method the method is using parameter less constructor of the given page. To pass parameters to page when navigating, use [this](https://social.msdn.microsoft.com/Forums/en-US/8cb42356-82bc-4d77-9bbc-ae186990cfd5/passing-parameters-during-navigation-in-windows-8?forum=winappswithcsharp)... – Marian Dolinský Feb 22 '17 at 21:22
  • I'm working on UWP for Windows 10. The solution you suggested is for Windows 8 apps. @MarianDolinský – Jestin Feb 23 '17 at 13:26
  • I know, but it works the same in UWP – Marian Dolinský Feb 23 '17 at 13:31
  • Then how do I put OnNavigatedTo event? @MarianDolinský – Jestin Feb 23 '17 at 13:40
  • I've updated the question @renévogt – Jestin Feb 23 '17 at 13:53

1 Answers1

0

This is happening because the Frame.Navigate method that is used for navigation uses parameter less constructor of the given page. To pass parameters to the target page you should use this overload of the method.

void Submit()
{
    DateTime? pickupdate = PickupDate.Date.DateTime;
    DateTime? retdate    = ReturnDate.Date.DateTime;

    // This is useless, this instance is not used anywhere (Frame.Navigate creates its own new instance)
    // Reservation res = new Reservation(pickupdate.Value.ToString("dd-MM-yyyy"), retdate.Value.ToString("dd-MM-yyyy"));

    string[] parameters = { pickupdate.Value.ToString("dd-MM-yyyy"), retdate.Value.ToString("dd-MM-yyyy") };
    Frame.Navigate(typeof(Reservation), parameters);
}

Reservation page:

string[] parameters;

public Reservation()
{
    Loaded += (sender, e) =>
    {
        PickupDateDisplay.Text = parameters[0];
        ReturnDateDisplay.Text = parameters[1];
    }

    InitializeComponent();
}

// This ctor is useless too
// public Reservation(string pickupdate,string retdate)
// {
//    InitializeComponent();
//    PickupDateDisplay.Text = pickupdate;
//    ReturnDateDisplay.Text = retdate;
// }

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    parameters = (string[])e.Parameter;
}
Marian Dolinský
  • 3,076
  • 3
  • 13
  • 29