summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla42069.cs
blob: d4de5aa372e45c18d9afdb2a35082df96f0d2865 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using Xamarin.Forms.Controls.Issues;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Controls
{
	[Preserve(AllMembers = true)]
	[Issue(IssueTracker.Bugzilla, 42069, "Garbage Collector can not collect pages that use ImageSource as a StaticResource",
		PlatformAffected.All)]
	public class Bugzilla42069 : TestNavigationPage
	{
		protected override void Init()
		{
			if (Application.Current.Resources == null)
			{
				Application.Current.Resources = new ResourceDictionary();
			}

			if (!Application.Current.Resources.ContainsKey("SomeSmallImage"))
			{
				ImageSource smallImage;
				switch (Device.RuntimePlatform) {
				default:
					smallImage = "coffee.png";
					break;
				case Device.WinPhone:
				case Device.Windows:
					smallImage = "bank.png";
					break;
				}

				Application.Current.Resources.Add("SomeSmallImage", smallImage);
			}

			const string instructions1 = @"Tap the Start button and follow the instructions on the next page.";
			string instructions2 =
				$"When you return to this page, tap the Collect button. The message \n'{Bugzilla42069_Page.DestructorMessage}'\n should appear at least once in the debug output.";

			var label1 = new Label { Text = instructions1 };
			var label2 = new Label { Text = instructions2, HorizontalTextAlignment = TextAlignment.Center };

			var startButton = new Button { Text = "Start" };
			startButton.Clicked += (sender, args) =>
			{
				// We have to do the push-pop-push dance because NavigationPage
				// holds a reference to its last page for unrelated reasons; our concern 
				// here is that the first Bugzilla42069_Page that we pushed gets collected
				PushAsync(new Bugzilla42069_Page(), false);
				PopAsync(false);
				PushAsync(new Bugzilla42069_Page(), false);
			};

			var collectButton = new Button { Text = "Collect" };
			collectButton.Clicked += (sender, args) =>
			{
				GC.Collect();
				GC.Collect();
				GC.Collect();
			};

			var startPage = new ContentPage
			{
				Content = new StackLayout
				{
					Children =
					{
						label1,
						startButton,
						label2,
						collectButton
					}
				}
			};

			PushAsync(startPage);
		}
	}
}