summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls/GalleryPages/PlatformSpecificsGalleries/MasterDetailPageWindows.cs
blob: d862c16750fba07492465080649999866cf705be (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Input;
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.WindowsSpecific;
using static Xamarin.Forms.Controls.GalleryPages.PlatformSpecificsGalleries.WindowsPlatformSpecificsGalleryHelpers;

namespace Xamarin.Forms.Controls.GalleryPages.PlatformSpecificsGalleries
{
    public class MasterDetailPageWindows : MasterDetailPage
	{
		public MasterDetailPageWindows(ICommand restore)
		{
			On<Windows>()
				.SetCollapseStyle(CollapseStyle.Partial);
			MasterBehavior = MasterBehavior.Popover;

			var master = new ContentPage { Title = "Master Detail Page" };
			var masterContent = new StackLayout { Spacing = 10, Margin = new Thickness(0, 10, 5, 0) };
			var detail = new ContentPage { Title = "This is the detail page's Title" };

			// Build the navigation pane items
			var navItems = new List<NavItem>
			{
				new NavItem("Display Alert", "\uE171", new Command(() => DisplayAlert("Alert", "This is an alert", "OK"))),
				new NavItem("Return To Gallery", "\uE106", restore),
				new NavItem("Save", "\uE105", new Command(() => DisplayAlert("Save", "Fake save dialog", "OK"))),
				new NavItem("Audio", "\uE189", new Command(() => DisplayAlert("Audio", "Never gonna give you up...", "OK"))),
				new NavItem("Set Detail to Navigation Page", "\uE16F", new Command(() => Detail = CreateNavigationPage())),
				new NavItem("Set Detail to Content Page", "\uE160", new Command(() => Detail = detail)),
			};

			var navList = new NavList(navItems);

			// And add them to the navigation pane's content
			masterContent.Children.Add(navList);
			master.Content = masterContent;
			
			var detailContent = new StackLayout { VerticalOptions = LayoutOptions.Fill, HorizontalOptions = LayoutOptions.Fill };
			detailContent.Children.Add(new Label
			{
				Text = "Platform Features",
				FontAttributes = FontAttributes.Bold,
				HorizontalTextAlignment = TextAlignment.Center,
				VerticalTextAlignment = TextAlignment.Center
			});

			detailContent.Children.Add(CreateCollapseStyleChanger(this));
			detailContent.Children.Add(CreateToolbarPlacementChanger(this));
			detailContent.Children.Add(CreateCollapseWidthAdjuster(this));
			detailContent.Children.Add(CreateAddRemoveToolBarItemButtons(this));

			detail.Content = detailContent;

			Master = master;

			AddToolBarItems(this);

			Detail = detail;
		}

		static Layout CreateCollapseStyleChanger(MasterDetailPage page)
		{
			Type enumType = typeof(CollapseStyle);

			return CreateChanger(enumType,
				Enum.GetName(enumType, page.On<Windows>().GetCollapseStyle()),
				picker =>
				{
					page.On<Windows>().SetCollapseStyle((CollapseStyle)Enum.Parse(enumType, picker.Items[picker.SelectedIndex]));
				},
				"Select Collapse Style");
		}

		static Layout CreateCollapseWidthAdjuster(MasterDetailPage page)
		{
			var adjustCollapseWidthLabel = new Label
			{
				Text = "Adjust Collapsed Width",
				VerticalTextAlignment = TextAlignment.Center,
				VerticalOptions = LayoutOptions.Center
			};
			var adjustCollapseWidthEntry = new Entry { Text = page.On<Windows>().CollapsedPaneWidth().ToString() };
			var adjustCollapseWidthButton = new Button { Text = "Change", BackgroundColor = Color.Gray };
			adjustCollapseWidthButton.Clicked += (sender, args) =>
			{
				double newWidth;
				if (double.TryParse(adjustCollapseWidthEntry.Text, out newWidth))
				{
					page.On<Windows>().CollapsedPaneWidth(newWidth);
				}
			};

			var adjustCollapsedWidthSection = new StackLayout
			{
				HorizontalOptions = LayoutOptions.Center,
				Orientation = StackOrientation.Horizontal,
				Children = { adjustCollapseWidthLabel, adjustCollapseWidthEntry, adjustCollapseWidthButton }
			};

			return adjustCollapsedWidthSection;
		}

		public class NavItem
		{
			public NavItem(string text, string icon, ICommand command)
			{
				Text = text;
				Icon = icon;
				Command = command;
			}

			public ICommand Command { get; set; }

			public string Icon { get; set; }

			public string Text { get; set; }
		}

		public class NavList : ListView
		{
			public NavList(IEnumerable<NavItem> items)
			{
				ItemsSource = items;
				ItemTapped += (sender, args) => (args.Item as NavItem)?.Command.Execute(null);

				ItemTemplate = new DataTemplate(() =>
				{
					var grid = new Grid();
					grid.ColumnDefinitions.Add(new ColumnDefinition { Width = 48 });
					grid.ColumnDefinitions.Add(new ColumnDefinition { Width = 200 });

					grid.Margin = new Thickness(0, 10, 0, 10);

					var text = new Label
					{
						VerticalOptions = LayoutOptions.Fill
					};
					text.SetBinding(Label.TextProperty, "Text");

					var glyph = new Label
					{
						FontFamily = "Segoe MDL2 Assets",
						FontSize = 24,
						HorizontalTextAlignment = TextAlignment.Center
					};

					glyph.SetBinding(Label.TextProperty, "Icon");

					grid.Children.Add(glyph);
					grid.Children.Add(text);

					Grid.SetColumn(glyph, 0);
					Grid.SetColumn(text, 1);

					grid.WidthRequest = 48;

					var cell = new ViewCell
					{
						View = grid
					};

					return cell;
				});
			}
		}

		static NavigationPage CreateNavigationPage()
		{
			var page = new NavigationPage { Title = "This is the Navigation Page Title" };

			page.PushAsync(CreateNavSubPage());

			return page;
		}

		static ContentPage CreateNavSubPage()
		{
			var page = new ContentPage();

			var label = new Label { Text = "This is content in a nav page" };
			var button = new Button() { Text = "Push Another Page"};

			button.Clicked += (sender, args) => page.Navigation.PushAsync(CreateNavSubPage());

			page.Content = new StackLayout { Children = { label, button } };

			return page;
		}
	}
}