Bug 27378 - Navigation.InsertPageBefore causes AurgumentException only on Windows Phone
Summary: Navigation.InsertPageBefore causes AurgumentException only on Windows Phone
Alias: None
Product: Forms
Classification: Xamarin
Component: Windows ()
Version: 1.4.2
Hardware: Macintosh Windows
: Normal major
Target Milestone: ---
Assignee: Eric Maupin
Depends on:
Reported: 2015-02-24 16:01 UTC by Carl Barton
Modified: 2015-07-20 13:56 UTC (History)
9 users (show)

Tags: ac
Is this bug a regression?: ---
Last known good build:

Solution showing issue (257.74 KB, application/zip)
2015-02-24 16:01 UTC, Carl Barton

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Carl Barton 2015-02-24 16:01:57 UTC
Created attachment 10012 [details]
Solution showing issue

When trying to manipulate the Navigation Stack programatically an ArgumentException occurs on Windows Phone however works as expected on iOS and Android.

My environment is:

Machine: MacBook Pro Early 2011
OS: Yosemite 10.10.2
VM: Parallels 10.1.4
VM OS: Windows 8.1 Pro 64-bit
Xamarin for Windows: 3.9.302.0 (Stable)

Basic Forms app with two XAML forms. App.cs loads one form. A button on the form attempts to insert the other form first in the stack and then PopAsync()s itself leaving the new form as the only one on the stack. Works as expected on iOS and Android but Windows throws the following exception when InsertPageBefore is called:

Exception: System.ArgumentException: before must be in the current navigation context
   at Xamarin.Forms.NavigationModel.InsertPageBefore(Page page, Page before)
   at Xamarin.Forms.Platform.WinPhone.Platform.InsertPageBefore(Page page, Page before)
   at Xamarin.Forms.Platform.WinPhone.NavigationPageRenderer.ElementOnInsertPageBeforeRequested(Object sender, NavigationRequestedEventArgs eventArgs)
   at Xamarin.Forms.NavigationPage.InsertPageBefore(Page page, Page before)
   at Xamarin.Forms.NavigationPage.NavigationImpl.OnInsertPageBefore(Page page, Page before)
   at Xamarin.Forms.NavigationProxy.InsertPageBefore(Page page, Page before)
   at Xamarin.Forms.NavigationProxy.OnInsertPageBefore(Page page, Page before)
   at Xamarin.Forms.NavigationProxy.InsertPageBefore(Page page, Page before)
   at WPNavStackBug.MyOneTimePage.<<.ctor>b__0>d__2.MoveNext()

Very basic sample is attached.
Comment 1 George Josh Karabin 2015-03-18 10:03:50 UTC
I've run into this problem as well.  I'm running Xamarin Forms

I can add one detail that may be of interest.  Well after starting the system and pushing many pages onto the navigation stack, my application works fine with the above approach to put pages onto the stack.  The code looks like this, assuming that the application's MainPage is a NavigationPage.

InsertPageBefore(someNewPage, ((NavigationPage)Xamarin.Forms.Application.Current.MainPage).CurrentPage);

However, if I try to use the same technique shortly after starting the app, on the first page that was pushed onto the stack, I see the same problem described by the original reporter.  I wonder if the position on the navigation stack has anything to do with the issue?
Comment 2 George Josh Karabin 2015-03-18 10:04:57 UTC
(And of course, I'm following the InsertPageBefore call with a PopAsync().  It doesn't matter whether the PopAsync is with animations or without in the problem case.
Comment 3 derek_papworth 2015-05-03 06:30:31 UTC
I have this same problem and running latest Xamarin Forms

And also as George mentions, it only seems to occur after app has first started with first page pushed on stack .. it's almost as though first page doesn't set something (the "before" in exception message?) whereas subsequent stack operations do?

Any news on this greatly appreciated as it screws up using the app as they terminate with the exception.
Comment 4 derek_papworth 2015-05-03 06:31:46 UTC
p.s. Or workaround we could use for now?
Comment 5 derek_papworth 2015-05-03 06:42:24 UTC
Bit more info ...

The issue doesn't always happen .. if I run my app which has an option at start up that does an InsertPageBefore action followed by popasync it sometimes works but sometimes I get the exception and terminates .. so it's possible there's some variable not being initialised maybe for the first page on the stack (which subsequent push/pops do set/reset) and hence sometimes it starts ok but others not?
Comment 6 Adam 2015-05-09 01:14:06 UTC
Ran into the exact same issue, I can confirm this is a bug. Still trying to get a workaround happening.
Comment 7 George Josh Karabin 2015-05-11 10:10:38 UTC
My WinPhone workaround is to create a compound operation to await PushAsync the new Page, and then call RemovePage on the original Page.  The first page is an empty ContentPage, and I PushAsync with no animation, so it's not too ugly.

 If you are using the back button, you might want to think about how to handle OnBackButtonPressed - I choose to ignore it during the compound PushAsync-RemovePage operation.

Seems OK so far, but I'd rather have a reliable InsertPageBefore, since I use it elsewhere in the app, and if it's not tested in this case, it could easily become unreliable in other untested cases as well.
Comment 8 derek_papworth 2015-06-11 05:28:04 UTC
Is there any news on possible fix for this yet? This is a problem as can't publish an app to the store that aborts due to this.

Comment 9 Rui Marinho 2015-06-26 11:42:18 UTC
Should be fixed in 1.4.4
Comment 10 Parmendra Kumar 2015-07-20 13:56:07 UTC
I have checked this issue with X.F 1.4.4-pre2 and its working fine at my end.

Screencast: http://www.screencast.com/t/16qcAL8gfpbm

Hence closing this issue.