summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue2266.cs
blob: 47b0091ba5c8fd0c56e2ad78bdc09ac997048b84 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using Xamarin.Forms;
using Xamarin.Forms.CustomAttributes;
using System.Collections.Generic;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Controls
{
#if APP
	[Preserve (AllMembers = true)]
	[Issue (IssueTracker.Github, 2266, "Setting a different Detail page from a MasterDetailPage after 2nd time on MainPage", PlatformAffected.iOS, NavigationBehavior.SetApplicationRoot)]
	public class Issue2266 : ContentPage
	{
		public Issue2266 ()
		{
			InitPageContent ();
		}

		void InitPageContent ()
		{
			var labelHeader = new Label {
				Text = "Select a test",
				FontSize = 30,
				FontAttributes = FontAttributes.Bold,
				VerticalOptions = LayoutOptions.Center,
				HorizontalOptions = LayoutOptions.CenterAndExpand
			};

			string[] listItems = {
				"MasterDetail Navigation",
				"MasterDetail Navigation ->> Page 1",
				"MasterDetail Navigation ->> Page 2",
				"MasterDetail Navigation ->> Page 3"
			};

			var listView = new ListView {
				ItemsSource = listItems
			};

			Content = new StackLayout {
				Padding = new Thickness (0, 20, 0, 0),
				Children = {
					labelHeader,
					listView
				}
			};

			listView.ItemSelected += delegate(object sender, SelectedItemChangedEventArgs e) {
				if (e.SelectedItem == null)
					return;
				if (e.SelectedItem.Equals (listItems [0])) {
					Application.Current.MainPage = MasterDetailHost;
				} else if (e.SelectedItem.Equals (listItems [1]) || e.SelectedItem.Equals (listItems [2]) || e.SelectedItem.Equals (listItems [3])) {
					// MasterDetail Navigation - direct page open
					var item = e.SelectedItem.ToString ();
					var index = int.Parse (item.Substring (item.Length - 1)) - 1;
					Application.Current.MainPage = MasterDetailHost;
					MasterDetailHost.OpenPage (index);
				}

				listView.SelectedItem = null;
			};
		}

		static MasterDetailNavigation s_masterDetailHost;

		public static MasterDetailNavigation MasterDetailHost {
			get {
				if (s_masterDetailHost == null)
					s_masterDetailHost = new MasterDetailNavigation ();
				return s_masterDetailHost;
			}
		}

	}

	public class MasterDetailNavigation : MasterDetailPage
	{
		List<NavigationPage> _pages;

		public MasterDetailNavigation ()
		{
			InitPages ();

			var menuList = new ListView {
				BackgroundColor = Color.Transparent,
				ItemsSource = _pages,
				ItemTemplate = new DataTemplate (typeof(TextCell))
			};
			menuList.ItemTemplate.SetBinding (TextCell.TextProperty, "Title");

			Master = new ContentPage {
				BackgroundColor = Color.FromHex ("363636"),
				Title = "Menu",
				Content = menuList
			};

			Detail = new NavigationPage (new ContentPage { 
				Padding = new Thickness (20, 20),
				Content = new StackLayout { 
					Children = {
						new Label { Text = "Select a menu item" }
					}
				}
			});

			menuList.ItemSelected += (sender, e) => {
				var page = e.SelectedItem as NavigationPage;
				if (page != null) {
					Detail = page;
					IsPresented = false;
				}
			};
		}

		void InitPages ()
		{
			_pages = new List<NavigationPage> ();

			for (int i = 1; i <= 10; i++) {
				var btnSubPage = new Button { 
					Text = string.Format ("Open sub-page"), 
				};
				btnSubPage.Clicked += delegate {
					OpenSubPage (string.Format ("Sub for page: {0}", i));
				};
				var page = new ContentPage {
					Padding = new Thickness (20, 20),
					Title = string.Format ("Page {0}", i),
					Content = new StackLayout {
						Children = {
							new Label { Text = string.Format ("Page {0}", i) },
							btnSubPage
						}
					}
				};
				page.ToolbarItems.Add (new ToolbarItem ("START", null, delegate {
					Application.Current.MainPage = App.MenuPage;
				}));
				_pages.Add (new NavigationPage (page) { Title = page.Title });
			}
		}

		public void OpenPage (int index)
		{
			if (index >= _pages.Count) {
				// Index out of range
				return;
			}
			Detail = _pages [index];
		}

		async void OpenSubPage (string text)
		{
			await Detail.Navigation.PushAsync (new ContentPage {
				Content = new Label { Text = text }
			});
		}
	}
#endif
}