Bug 31262 - Seting Application.Current.MainPage to an existing Page causes the exception "Element is already the child of another element"
Summary: Seting Application.Current.MainPage to an existing Page causes the exception ...
Alias: None
Product: Forms
Classification: Xamarin
Component: Windows ()
Version: 1.4.3
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Jason Smith [MSFT]
Depends on:
Reported: 2015-06-19 17:08 UTC by Eder
Modified: 2015-07-21 13:13 UTC (History)
3 users (show)

Tags: ac windows phone silverlight
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 Eder 2015-06-19 17:08:58 UTC
With Xamarin.Forms running the app on a device Lumia 930 WP 8.1

If you try to set Application.Current.MainPage to a static instance of a Page you it will thow the following exception: "Element is already the child of another element"

follows a code sample that reproduces the bug.
** just click on the "Login" button (no need to type any username or password);
** and on the next page access the main menu;
** and click on the "Reproduce the bug" button to see what happens (running on WP);

On android it work correctly.
Comment 1 Seth Rosetter 2015-06-25 17:58:22 UTC
Hi Eder,

I am not seeing the sample you are referencing. Can you please attach it? Marking as NEEDSINFO for now. Please mark as NEW once you have provided the sample.   

Comment 2 Eder 2015-06-26 10:40:22 UTC
Follows the link to the sample I'm referencing (the zip file of the solution is too big to be attached on bugzilla).

Comment 4 Eder 2015-06-26 10:45:04 UTC
This sample is just to illustrate the problem.
On my real application I'm not storing the Pages instances as static members, instead I'm using autofac DI framework and registering my Pages as "SingleInstance" but for simplicity of reproduction of the problem I created a new project using Page instances stored (declared) on static members os a class.
Note that the issue just happen on WP, but work perfectly on Android Emulator.
Comment 5 Eder 2015-07-01 14:37:24 UTC
Is there a roadmap of when this bug will be squashed ?
Comment 6 Jason Smith [MSFT] 2015-07-14 22:16:43 UTC
Comment 7 Jason Smith [MSFT] 2015-07-14 23:11:55 UTC
There were a series of issues this repro brought up. Technically the pages were being multi-parented because the MDP was not checking that the page was not already parented. This has now been fixed which would cause this repro to throw an exception now that makes more sense.

However the root issue is also fixed now. If you either retain the MDP used in the repro or somehow deparent that static master and detail before adding them to a new page, it will work as expected.