summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls/ControlGalleryPages/AppearingGalleryPage.cs
blob: 21d91906ea4ab7f0bb6ab9db90ca0ebdbcd775bc (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;

using Xamarin.Forms;
using System.Diagnostics;
using System.Collections.Generic;

namespace Xamarin.Forms.Controls
{
	public class AppearingGalleryPage : ContentPage
	{
		const string NavPageTitle = "NavAppearingPage";
		const string MasterPageTitle = "MasterAppearingPage";
		const string TabbedPageTitle = "TabbedAppearingPage";
		const string CarouselPageTitle = "CarouselAppearingPage";

		public AppearingGalleryPage ()
		{
			var initalPage = new AppearingPage (1);
			var initalPage2 = new AppearingPage (2);

			Content = new StackLayout { 
				Children = {
					new Button { Text = NavPageTitle, Command = new Command (() => {
						Application.Current.MainPage = new NavAppearingPage(initalPage);
						}) 
					},
					new Button { Text = MasterPageTitle, Command = new Command (() => {
						var page = new MasterDetailPage {
							Title = MasterPageTitle,
							Master = new ContentPage { Title = "Master", BackgroundColor = Color.Red },
							Detail =  new NavAppearingPage(initalPage)
						};
						SetMainPage (page);
					})
					},
					new Button { Text = TabbedPageTitle, Command = new Command (() => {
						var page = new TabbedPage {
							Title = TabbedPageTitle,
							Children = { initalPage, initalPage2 }
						};
						SetMainPage (page);
					})
					},
					new Button { Text =  CarouselPageTitle, Command = new Command (() => {
						
						var page = new CarouselPage {
							Title = CarouselPageTitle,
							Children = { initalPage, initalPage2 }
						};
						SetMainPage (page);
					})
					}	
				}
			};
		}

		static void SetMainPage (Page page)
		{
			var tracker = new AppearingTracker (page);
			Application.Current.MainPage = page;
		}

		class AppearingTracker 
		{
			int _isAppearingFired;
			int _isDisappearingFired;

			public AppearingTracker (Page page)
			{
				page.Appearing += (object sender, EventArgs e) => {
					_isAppearingFired++;
					App.AppearingMessages.Insert (0, $"Appearing {page.Title}");
					Debug.WriteLine ($"Appearing {page.Title}");
				};

				page.Disappearing += (object sender, EventArgs e) => {
					_isDisappearingFired++;
					App.AppearingMessages.Insert (0, $"Disappearing {page.Title}");
					Debug.WriteLine( $"Disappearing {page.Title}");
				};
			}
		}

		class AppearingPage : ContentPage
		{
			int _theId;
			ListView _listMessages;
			public AppearingPage (int id)
			{
				var tracker = new AppearingTracker (this);
				_listMessages = new ListView ();
				_theId = id;
				Title = $"Page {_theId}";
				Padding = new Thickness (20);
				Content = new StackLayout {
					Children = {
						new Label { Text = $"Hello Appearing {_theId} page" },
						new Button { Text = "Push new Page", Command = new Command ( async () => { await Navigation.PushAsync( new AppearingPage(2)); }) },
						new Button { Text = "Pop page", Command = new Command ( async () => { await Navigation.PopAsync(); }) },
						new Button { Text = "Pop to root", Command = new Command ( async () => { await Navigation.PopToRootAsync(); }) },
						new Button { Text = "Change Main Page", Command = new Command ( () => { 
							App.AppearingMessages.Clear();
							Application.Current.MainPage = new AppearingPage(3); }) 
						},
						_listMessages
					}
				};
			}
			protected override void OnAppearing ()
			{
				base.OnAppearing ();
				Device.StartTimer (new TimeSpan (200), () => {
					_listMessages.ItemsSource = App.AppearingMessages;
					return false;
				});
			}
		}

		class NavAppearingPage : NavigationPage
		{
			public NavAppearingPage (Page page) : base(page)
			{
				Title = NavPageTitle;
				var tracker = new AppearingTracker (this);
			}
		}
	}
}