ArgumentNullException when changing frame

So, I'm trying to change the frames in a Windows 8 application. I tried to complete the tutorial on this page , but I still get the same error.

I get an ArgumentNullException in the line:

frameState[_pageKey] = pageState; 

in the LayoutAwarePage.cs class in the OnNavigatedFrom method.

Now I'm not sure why I get this error, because I feel that there is nothing in my code that could cause it. My onclick button has this code:

 DateTime chosenDateTime = new DateTime(year, month, day, hours, minutes, seconds); this.Frame.Navigate(typeof(MainPage), chosenDateTime.ToString()); 

And the OnNavigatedTo method in my MainPage looks like this:

 protected override void OnNavigatedTo(NavigationEventArgs e) { string parameter = (string)e.Parameter; if (parameter != "") { Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings; roamingSettings.Values["chosenDateTime"] = parameter; chosenDateTime = Convert.ToDateTime(e.Parameter); } else { Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings; if (roamingSettings.Values.ContainsKey("chosenDateTime")) { chosenDateTime = Convert.ToDateTime(roamingSettings.Values["chosenDateTime"].ToString()); } if (roamingSettings.Values.ContainsKey("headline")) { chosenDateTextBlock.Text = roamingSettings.Values["headline"].ToString(); } } SetTime(); } 

Can someone give me some information on how I can solve this?

Thanks.

+7
source share
2 answers

Ok, so I found the answer to my question!

On both pages that I link to, I had to implement at least a minimal implementation of two methods:

 protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); } protected override void OnNavigatedFrom(NavigationEventArgs e) { base.OnNavigatedFrom(e); } 

AND

 base.OnNavigatedFrom(e); base.OnNavigatedTo(e); 

were very important in the methods.

+10
source

Another situation that can provoke this problem is that the page is no longer attached to any frame (for example, it is still in memory due to event handlers or other links, but its frame has already been moved from the page).

It is very simple to do this by accident if you call Frame.Navigate() in an event handler that can fire several times or join the event several times (the first call will work, and the second will be called after the page no longer has a frame).

0
source

All Articles