Bug 24686 - [iOS] Setting the MainPage with a static reference to a page fails
Summary: [iOS] Setting the MainPage with a static reference to a page fails
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.3.0
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2014-11-20 05:21 UTC by Tim Brand
Modified: 2014-12-04 21:29 UTC (History)
5 users (show)

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

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 Tim Brand 2014-11-20 05:21:22 UTC
Platform: iOS

When setting the Application.Current.MainPage property with a static reference to a ContentPage that has been initialised before the application output shows the error: 

XamarinFormsTestsiOS[47400:782327] Potential view double add

1: Set in App.cs a ContentPage to the MainPage and set this contentpage to a static field
2: Open a new ContentPage by setting the Application.Current.MainPage with another contentpage
3: Now open the first ContentPage again by setting the Application.Current.MainPage with the static reference from step-1;

I've created a test application to simulate this on https://github.com/App-Artment/xamarin-forms-tests
Inside this application:
- tap: 'Tap here to start'
- tap: Startpage
Error appears.
Comment 1 Udham Singh 2014-11-21 09:05:54 UTC
I have checked this issue with the help of instructions provided in bug description and observed that when we click on option 'Startpage' it does not redirect to the page 'BasicStartPage' and also in application output it shows message "XamarinFormsTestsiOS[6508:146743] Potential view double add".

Screencast : http://www.screencast.com/t/0DPwhx379P

Please let me know if I missed anything or have check something else to see the same behaviour which you are getting.

Environment Info :

=== Xamarin Studio ===

Version 5.5.4 (build 15)
Installation UUID: ce927b2a-2c07-44c5-b186-09cfdafba6dc
	Mono 3.10.0 ((detached/92c4884)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 310000031

=== Xamarin.Android ===

Version: (Enterprise Edition)
Android SDK: /Users/xamarin76/Desktop/android-sdk-macosx
	Supported Android versions:
		1.6    (API level 4)
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.0    (API level 11)
		3.1    (API level 12)
		3.2    (API level 13)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

=== Apple Developer Tools ===

Xcode 6.1 (6602)
Build 6A1052c

=== Xamarin.Mac ===

Version: (Enterprise Edition)

=== Xamarin.iOS ===

Version: (Enterprise Edition)
Hash: 840a925
Build date: 2014-11-16 21:03:22-0500

=== Build Information ===

Release ID: 505040015
Git revision: f93940a35458a18052f1a25e106e62ca970d9c40
Build date: 2014-11-19 15:32:41-05
Xamarin addins: dc23cbd91a3a0e1d326328e1229e86c942a49ec8

=== Operating System ===

Mac OS X 10.9.4
Darwin Xamarin76s-Mac-mini.local 13.3.0 Darwin Kernel Version 13.3.0
    Tue Jun  3 21:27:35 PDT 2014
    root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64
Comment 2 Tim Brand 2014-11-25 09:01:53 UTC
Not sure what the above response to this ticket means, are you saying that you can or can't reproduce? As it's not workin on iOS, as you discovered too.

I can confirm the same behaviour on Windows Phone.
Added a WinPhone project (in a separate Solution file) to the test project.

Just follow the same steps:
- Open the app
- tap: "tap here to start"
- tap: "Startpage"
An exception on windows phone occurs:

System.InvalidOperationException was unhandled by user code
  Message=Element is already the child of another element.
       at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
       at MS.Internal.XcpImports.Collection_AddValue[T](PresentationFrameworkCollection`1 collection, CValue value)
       at MS.Internal.XcpImports.Collection_AddDependencyObject[T](PresentationFrameworkCollection`1 collection, DependencyObject value)
       at System.Windows.PresentationFrameworkCollection`1.AddDependencyObject(DependencyObject value)
       at System.Windows.Controls.UIElementCollection.AddInternal(UIElement value)
       at System.Windows.PresentationFrameworkCollection`1.Add(T value)
       at Xamarin.Forms.Platform.WinPhone.Platform.SetCurrent(Page page, Boolean animated, Boolean popping, Action completedCallback)
       at Xamarin.Forms.Platform.WinPhone.Platform.SetPage(Page newRoot)
       at Xamarin.Forms.Forms.ConvertPageToUIElementWithoutApp(Page page, PhoneApplicationPage applicationPage)
       at Xamarin.Forms.Platform.WinPhone.FormsApplicationPage.SetMainPage()
       at Xamarin.Forms.Platform.WinPhone.FormsApplicationPage.ApplicationOnPropertyChanged(Object sender, PropertyChangedEventArgs args)
       at Xamarin.Forms.BindableObject.OnPropertyChanged(String propertyName)
       at Xamarin.Forms.Application.set_MainPage(Page value)
       at XamarinFormsTests.App.ShowStartPage()
       at XamarinFormsTests.Views.MainMenuPage.<>c__DisplayClass4.<InitPageContent>b__3(Object sender, SelectedItemChangedEventArgs e)
       at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
       at Xamarin.Forms.ListView.OnSelectedItemChanged(BindableObject bindable, Object oldValue, Object newValue)
       at Xamarin.Forms.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent)
       at Xamarin.Forms.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes)
       at Xamarin.Forms.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes)
       at Xamarin.Forms.ListView.NotifyRowTapped(Int32 groupIndex, Int32 inGroupIndex)
       at Xamarin.Forms.ListView.NotifyRowTapped(Int32 index)
       at Xamarin.Forms.Platform.WinPhone.ListViewRenderer.OnNativeSelectionChanged(Object sender, SelectionChangedEventArgs e)
       at Microsoft.Phone.Controls.LongListSelector.set_SelectedItem(Object value)
       at Microsoft.Phone.Controls.LongListSelector.OnItemTap(Object sender, GestureEventArgs e)
       at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
Comment 3 Jason Smith [MSFT] 2014-12-04 21:29:41 UTC
Fixed in next release