Template10 icon indicating copy to clipboard operation
Template10 copied to clipboard

An exception of type 'System.Runtime.InteropServices.COMException'

Open ivesely opened this issue 10 years ago • 4 comments

An exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.ni.dll but was not handled in user code

WinRT information: The BackStack or ForwardStack cannot be changed while navigating.

Additional information: The operation identifier is not valid.



The BackStack or ForwardStack cannot be changed while navigating.

If there is a handler for this exception, the program may be safely continued.

Steps to induce: In minimal sample in method OnNavigatingFrom set args.cancel to true.

public override void OnNavigatingFrom(NavigatingEventArgs args)
{
    args.Cancel = true;
}

Exception occurs in HamburgerMenu->ExecuteNav->ClearHistory();

The exception is caused by clearing backstack in navigation process.

ivesely avatar Jan 06 '16 19:01 ivesely

Possible sollution is to cancel navigation before calling FrameFacade.Navigate(page, parameter, infoOverride); in navigation service.

public bool Navigate(Type page, object parameter = null, NavigationTransitionInfo infoOverride = null)
{
 ...
    //new
   var currentPage = FrameFacade.Content as Page;
   var dataContext = currentPage?.DataContext as INavigable;
   if (dataContext != null)
   {
       var args = new NavigatingEventArgs
       {
            NavigationMode = FrameFacade.NavigationModeHint,
            PageType = FrameFacade.CurrentPageType,
            Parameter = FrameFacade.CurrentPageParam,
            Suspending = false,
       };
       dataContext.BeforeNavigatingFrom(args);
       if (args.Cancel) {return false;} 
    }


    parameter = SerializePageParam(parameter);
    return FrameFacade.Navigate(page, parameter, infoOverride);
}

Ideal would be if the method Navigate was async and BeforeNavigatingFrom was async too. I implemented it in my local copy and it works well and as a bonus I can use async dilaogs in BeforeNavigatingFrom event.

ivesely avatar Jan 06 '16 19:01 ivesely

This would be a great pull request. Would you submit it?

JerryNixon avatar Jan 07 '16 20:01 JerryNixon

#478

ivesely avatar Jan 10 '16 16:01 ivesely

Yeah! I fixed it.

JerryNixon avatar Jan 15 '16 23:01 JerryNixon