This project is read-only.

OnNavigatingFrom cancellation after exception Handling

Oct 3, 2014 at 9:49 AM

In my Project, there is some validation code that is performed on the OnNavigatingFrom event handler.

If validation fails we cancel the navigation setting e.Cancel = true. This works great for most cases.

However, I have some validation code that is inside a try...catch block. If there is any exception, I want to also cancel the navigation.

However, this is not working. Even after setting e.Cancel = true on the catch block, the navigation takes place and it is not cancelled. It seems this happens only when a exception handling is involved.

Is there anything I should know about that?

Thank you,

Oct 3, 2014 at 8:23 PM
Setting e.Cancel in a try.. catch block should work equally well. Care to share some source code?
Oct 6, 2014 at 4:05 PM
Thank you for the reply,

I double checked my code and the try.. catch block is not the problem.

The problem is that I have some async await calls in the OnNavigatingFrom method and even so the UI framework seems to process the message. Even if we cancel the event (after the async task is completed) the navigation was already done.

However, I understand that blocking the UI until the async task is completed does not make a responsive UI. So I guess I will have to make this processing at another moment.

Thank you anyway for the support

Oct 6, 2014 at 9:07 PM
That's a correct observation. If you want to cancel the navigation, it needs to be done in a synchronous way. Awaiting a task will allow the ModernFrame navigation to continue, setting the Cancel afterwards won't have any effect. Blocking the UI is generally not a great idea.