summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla27350.cs
blob: e48993587a8ad49a8dd109b846c717ee5223d7ad (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
using System;

using Xamarin.Forms.CustomAttributes;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls
{
	[Preserve (AllMembers = true)]
	[Issue (IssueTracker.Bugzilla, 27350, "Binding throws Null Pointer Exception when Updating Tab")]
	public class Bugzilla27350 : TestContentPage // or TestMasterDetailPage, etc ...
	{
		protected override void Init ()
		{
			var btn = new Button { Text = "next main" };
			btn.Clicked += async (object sender, EventArgs e) => await Navigation.PushAsync (new MainPage1 ());
			Content = btn;
		}

		class RecipeViewModel
		{
			public Recipe Recipe { get; set; }

			public ObservableCollection<RecipeGroup> RecipeGroups { get; set; }

			public async Task LoadRecipesAsync ()
			{
				var groups = new  ObservableCollection<RecipeGroup> ();
				groups.Add (new RecipeGroup { Title = "Teste 1" });
				groups.Add (new RecipeGroup { Title = "Teste 2" });
				groups.Add (new RecipeGroup { Title = "Teste 3" });
				groups.Add (new RecipeGroup { Title = "Teste 4" });
				groups.Add (new RecipeGroup { Title = "Teste 5" });
				groups.Add (new RecipeGroup { Title = "Teste 6" });
				groups.Add (new RecipeGroup { Title = "Teste 4" });
				groups.Add (new RecipeGroup { Title = "Teste 5" });
				groups.Add (new RecipeGroup { Title = "Teste 6" });

				RecipeGroups = groups;
			}
		}

		class Recipe
		{
			public string ID { get; set; }

			public string Title { get; set; }

			public string Subtitle { get; set; }

			public string Description { get; set; }

			public string ImagePath { get; set; }

			public string TileImagePath { get; set; }

			public int PrepTime { get; set; }

			public string Directions { get; set; }

			public List<string> Ingredients { get; set; }
		}

		class RecipeGroup : INotifyPropertyChanged
		{
			public event PropertyChangedEventHandler PropertyChanged;

			void OnPropertyChanged (string caller)
			{
				var handler = PropertyChanged;
				if (handler != null) {
					handler (this, new PropertyChangedEventArgs (caller));
				}
			}

			public string ID { get; set; }

			public string Title {
				get{ return _title; }
				set {
					_title = value;
					OnPropertyChanged ("Title");
				}
			}

			string _title;

			public string Subtitle { get; set; }

			public string ImagePath { get; set; }

			public string GroupImagePath { get; set; }

			public string Description { get; set; }

			public List<Recipe> Recipes { get; set; }
		}

		class MainPage1 : TabbedPage
		{
			public MainPage1 ()
			{
				ItemTemplate = new DataTemplate (() => {
					var page = new ContentPage ();
					page.SetBinding (TitleProperty, new Binding ("Title"));
					var btn = new Button { Text = "change", Command = new Command (() => {
							(page.BindingContext as RecipeGroup).Title = "we changed";
						})
					};
					var btn1 = new Button { Text = "null", Command = new Command (() => {
							(page.BindingContext as RecipeGroup).Title = null;
						})
					};
					page.Content = new StackLayout { Children = { btn, btn1 } };
					return page;
				});
				SetBinding (ItemsSourceProperty, new Binding ("RecipeGroups"));
			}

			protected override async void OnAppearing ()
			{
				base.OnAppearing ();

				if (BindingContext == null)
					BindingContext = await GetRecipeViewModelAsync ();
			}

			RecipeViewModel _rvm;

			public async Task<RecipeViewModel> GetRecipeViewModelAsync ()
			{
				if (_rvm == null) {
					_rvm = new RecipeViewModel ();
				} else {
					_rvm.RecipeGroups.Clear ();
				}

				await _rvm.LoadRecipesAsync ();

				return _rvm;
			}
		}
	}
}