diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla51503.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla51503.cs new file mode 100644 index 000000000..e9cdebc91 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla51503.cs @@ -0,0 +1,80 @@ +using System; +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + +#if UITEST +using Xamarin.UITest; +using NUnit.Framework; +#endif + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Bugzilla, 51503, "NullReferenceException on VisualElement Finalize", PlatformAffected.All)] + public class Bugzilla51503 : TestNavigationPage + { + protected override void Init() + { + PushAsync(new RootPage()); + } + + [Preserve(AllMembers = true)] + class RootPage : ContentPage + { + public RootPage() + { + Button button = new Button + { + AutomationId = "Button", + Text = "Open" + }; + + button.Clicked += Button_Clicked; + + Content = button; + } + + async void Button_Clicked(object sender, EventArgs e) + { + GC.Collect(); + GC.WaitForPendingFinalizers(); + + await Navigation.PushAsync(new ChildPage()); + } + } + + [Preserve(AllMembers = true)] + class ChildPage : ContentPage + { + public ChildPage() + { + Content = new Label + { + AutomationId = "VisualElement", + Text = "Navigate 3 times to this page", + Triggers = + { + new EventTrigger() + } + }; + } + } + +#if UITEST +[Test] + public void Issue51503Test() + { + for (int i = 0; i < 3; i++) + { + RunningApp.WaitForElement(q => q.Marked("Button")); + + RunningApp.Tap(q => q.Marked("Button")); + + RunningApp.WaitForElement(q => q.Marked("VisualElement")); + + RunningApp.Back(); + } + } +#endif + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index e040a049e..ba7de6cb0 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -222,6 +222,7 @@ + diff --git a/Xamarin.Forms.Core/VisualElement.cs b/Xamarin.Forms.Core/VisualElement.cs index cff1eb10b..352e25493 100644 --- a/Xamarin.Forms.Core/VisualElement.cs +++ b/Xamarin.Forms.Core/VisualElement.cs @@ -790,7 +790,7 @@ namespace Xamarin.Forms } if (!GetIsDefault(TriggersProperty)) { - var triggers = GetValue(TriggersProperty) as AttachedCollection; + var triggers = GetValue(TriggersProperty) as AttachedCollection; triggers.DetachFrom(this); } }