Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Changing a View from one parent to another crashes

$
0
0

When I execute this code with latest version (1.2.1.6229)

        ContentPage cp = new ContentPage ();
        Label l = new Label (){Text="test"};
        ContentPage cp2 = new ContentPage ();
        cp.Content = l;
        cp2.Content = l;

It crashes with this error:
System.NullReferenceException: Object reference not set to an instance of an object
at at Xamarin.Forms.NavigationProxy.set_Inner (Xamarin.Forms.INavigation)
at at Xamarin.Forms.VisualElement.OnParentSet ()
at at Xamarin.Forms.Element.set_Parent (Xamarin.Forms.Element)
at at Xamarin.Forms.Element.OnChildAdded (Xamarin.Forms.Element)
at at Xamarin.Forms.Page.OnInternalAdded (Xamarin.Forms.VisualElement)
at at Xamarin.Forms.Page.InternalChildrenOnCollectionChanged (object,System.Collections.Specialized.NotifyCollectionChangedEventArgs)
at at System.Collections.ObjectModel.ObservableCollection

1<Xamarin.Forms.Element>.OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs) <0x000f7>
  at at System.Collections.ObjectModel.ObservableCollection
1<Xamarin.Forms.Element>.InsertItem (int,Xamarin.Forms.Element) <0x000d3>
at at System.Collections.ObjectModel.Collection`1<Xamarin.Forms.Element>.Add (Xamarin.Forms.Element) <0x000c3>
at at Xamarin.Forms.ContentPage.set_Content (Xamarin.Forms.View)
at test.MainActivity.OnCreate (Android.OS.Bundle) [0x00039] in c:\Users\gerard.materna\Documents\Projects\test\test\MainActivity.cs:28
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/41933531/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Android.App.Activity.cs:1943
at at (wrapper dynamic-method) object.2fbe0b9f-2ad4-4d3f-98d7-5e437cf584dd (intptr,intptr,intptr)

I have seen this error (or the same like) on a lot of other posts. I went to see the code in Xamarin.Forms and I think I found a bug.

At the label creation, l.NavigationProxy.pushStack is initialized to
private Lazy<Stack> pushStack = new Lazy<Stack> (() => new Stack ());

When setting the label as the content of the first contentpage, l.NavigationProxy.pushStack is set to null:
VisualElement.OnParentSet () is called,
protected override void OnParentSet ()
{
base.OnParentSet ();
if (base.ParentView != null) {
this.NavigationProxy.Inner = base.ParentView.NavigationProxy;
return;
}
this.NavigationProxy.Inner = null;
}

in this.NavigationProxy.Inner:
[...] this.pushStack = null;

When the label is set as content of the second ContentPage, the same this.NavigationProxy.Inner crashes because it does assume that if NavigationProxy.Inner is not null then NavigationProxy.pushStack cannot be null (which is, IMHO, wrong):

    this.inner = value;
    if (object.ReferenceEquals (this.inner, null)) { //If inner is null, initialize pushStack, but Inner is not null in this case
        this.pushStack = new Lazy<Stack<Page>> (() => new Stack<Page> ());
        this.modalStack = new Lazy<Stack<Page>> (() => new Stack<Page> ());
        return;
    }
    if (this.pushStack.get_IsValueCreated ()) { //This crashes...
        foreach (Page current in this.pushStack.get_Value ().Reverse<Page> ()) {
            this.inner.PushAsync (current);
        }
    }

What do you think? Is this a wanted behaviour or a bug?


Viewing all articles
Browse latest Browse all 58056

Trending Articles