We use automated error reporting in our applications, and we were getting a few reports where the error was "The parameter is incorrect". We finally tracked it down to the back key being pressed very quickly when the page was being changed.
We decided that telling our users not to press the back key so quickly when pages were changing was not an option, so we had to find a way around it.
First of all, we tried overriding the Back key handling and putting a try…catch around the call to NavigationService.GoBack() but that didn’t do it. So, it was time for a re-think. The solution we came up, even we think is kludgey, but it does seem to get around the problem. Notice I say get around the problem, not solve it. A solution would be welcome.
If the page is fully loaded, then there is no problem, so we needed a way to prevent the the back key being pressed before the page was fully loaded. The 'kludgey' bit is to use timers to ensure the page is loaded. Until the page is fully loaded, effectively we ignore the back key.
First of all declare a couple of timers, and set them up at when the page loads.
DispatcherTimer tmrBackKey = new DispatcherTimer();
DispatcherTimer tmrFullyLoaded = new DispatcherTimer();
private static Boolean needBackKeyTimer = true;
tmrBackKey.Interval = new TimeSpan(0, 0, 1);
tmrBackKey.Tick += (sender, e) =>
tmrFullyLoaded.Interval = new TimeSpan(0, 0, 3);
tmrFullyLoaded.Tick += (sender, e) =>
Then, at the end of the OnNavigatedTo event, start the 'fully loaded' timer. We set this to 3 seconds, to allow the page to fully load - this is possibly excessive and could be tuned to your own application.
When the 'fully loaded' timer fires, we stop the timer and set the 'needBackKeyTimer' flag to false.
private void do_backkey_timer()
needBackKeyTimer = false;
We then need to handle the back key being pressed. If our 'needBackKeyTimer' is still set to true, our page is still not fully loaded, so we 'ignore' the key press, but if the flag is set to false, and we are fully loaded, we process the back key.
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
e.Cancel = true;
When the back key is pressed, if we are still not fully loaded, we start our 'back key' timer, and when that fires, we try to go back to our calling screen. If we are fully loaded, we then try to go back to our calling screen.
private void do_back_processing()
This seems to handle the problem effectively, although we don't feel this is a 'pretty' solution.
Jul 17 2011, 05:18 AM