summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls/ControlGalleryPages/NativeBindingGalleryPage.cs
blob: f0f404c74fa679948b6450e6cc4234fc51157912 (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
using System;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Controls
{
	public class NativeBindingGalleryPage : ContentPage
	{
		public StackLayout Layout { get; set; }
		public bool NativeControlsAdded { get; set; }

		NestedNativeViewModel ViewModel { get; set; }

		public const string ReadyForNativeBindingsMessage = "ReadyForNativeBindings";

		protected override void OnAppearing()
		{
			base.OnAppearing();
			MessagingCenter.Send(this, ReadyForNativeBindingsMessage);
		}

		public NativeBindingGalleryPage()
		{

			var vm = new NestedNativeViewModel();
			vm.FormsLabel = "Forms Label Binding";
			vm.NativeLabel = "Native Label Binding";
			vm.NativeLabelColor = Color.Red;
			vm.Age = 45;

			Layout = new StackLayout { Padding = 20, VerticalOptions = LayoutOptions.FillAndExpand };

			var buttonNav = new Button { Text = "New Page" };
			buttonNav.Clicked += (object sender, EventArgs e) =>
			{
				App.Current.MainPage = new ContentPage { Content = new Label { Text = "New page" } };
			};

			var button = new Button { Text = "Change BindingContext " };
			button.Clicked += (object sender, EventArgs e) =>
			{
				vm = new NestedNativeViewModel();
				vm.FormsLabel = "Forms Label Binding Changed";
				vm.NativeLabel = "Native Label Binding Changed";
				vm.NativeLabelColor = Color.Pink;
				vm.Age = 10;

				BindingContext = ViewModel = vm; ;
			};

			var boxView = new BoxView { HeightRequest = 50 };
			boxView.SetBinding(BoxView.BackgroundColorProperty, "NativeLabelColor");

			var label = new Label();
			label.SetBinding(Label.TextProperty, "FormsLabel");
			var labelAge = new Label();
			labelAge.SetBinding(Label.TextProperty, nameof(vm.Age));

			Layout.Children.Add(buttonNav);
			Layout.Children.Add(label);
			Layout.Children.Add(boxView);
			Layout.Children.Add(button);
			Layout.Children.Add(labelAge);

			BindingContext = ViewModel = vm; ;

			Content = new ScrollView { Content = Layout };
		}
	}


	[Preserve(AllMembers = true)]
	public class NestedNativeViewModel : ViewModelBase
	{
		string _formsLabel;
		public string FormsLabel
		{
			get { return _formsLabel; }
			set { if (_formsLabel == value) return; _formsLabel = value; OnPropertyChanged(); }
		}

		string _nativeLabel;
		public string NativeLabel
		{
			get { return _nativeLabel; }
			set { if (_nativeLabel == value) return; _nativeLabel = value; OnPropertyChanged(); }
		}

		Color _nativeLabelColor;
		public Color NativeLabelColor
		{
			get { return _nativeLabelColor; }
			set { if (_nativeLabelColor == value) return; _nativeLabelColor = value; OnPropertyChanged(); }
		}

		int _age;
		public int Age
		{
			get { return _age; }
			set { if (_age == value) return; _age = value; OnPropertyChanged(); }
		}

		bool _selected;
		public bool Selected
		{
			get { return _selected; }
			set { if (_selected == value) return; _selected = value; OnPropertyChanged(); }
		}
	}

}