summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls/GalleryPages/BoundContentPage.cs
blob: e3051fde37e3f11f67d7551beab2592c0b2d2cf8 (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 System.Globalization;
using System.Windows.Input;

namespace Xamarin.Forms.Controls
{
	[Preserve (AllMembers = true)]
	public class BoundContentPage : ContentPage
	{
        internal class NavWrapperConverter : IValueConverter
		{
			public object Convert (object value, Type targetType, object parameter, CultureInfo culture)
			{
				throw new NotSupportedException ();
			}

			public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture)
			{
				return new NavigationWrapper ((INavigation) value);
			}
		}

		internal class NavigationWrapper
		{
			// This class is dumb but proves you can wrap the INavigation with a converter to do some MVVM goodness
			// Normally this class would implement pushes with ViewModel then do the conversion
			readonly INavigation _inner;

			public NavigationWrapper (INavigation inner	)
			{
				_inner = inner;
			}

			public void WrappedPush ()
			{
				_inner.PushAsync (new ContentPage {
					Content = new StackLayout {
						BackgroundColor = Color.Red,
						Children = {
							new Label {
								Text = "Second Page"
							}
						}
					}
				});
			}
		}

		[Preserve (AllMembers = true)]
		internal class BoundContentPageViewModel
		{
			public string ButtonText { get { return "Click Me!"; } }

			public ICommand ButtonCommand { get; set; }

			public NavigationWrapper Navigation { get; set; }

			public BoundContentPageViewModel ()
			{
				ButtonCommand = new Command (() => Navigation.WrappedPush ());
			}
		}

		public BoundContentPage ()
		{
			Title ="Bound Gallery";

			BindingContext = new BoundContentPageViewModel ();

			var button = new Button ();
			button.SetBinding (Button.TextProperty, "ButtonText");
			button.SetBinding (Button.CommandProperty, "ButtonCommand");

			SetBinding (NavigationProperty, new Binding ("Navigation", converter: new NavWrapperConverter ()));

			Content = button;
		}
	}
}