summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue1545.xaml.cs
blob: 03679ac056cc517d8d2dc57bc61b8eeaf159dc4e (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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Controls
{
#if APP
	[Preserve (AllMembers = true)]
	[Issue (IssueTracker.Github, 1545, "Binding instances cannot be reused", PlatformAffected.Android | PlatformAffected.iOS | PlatformAffected.WinPhone)]
	public partial class Issue1545 : ContentPage
	{
		ArtistsViewModel _viewModel;
		public Issue1545()
		{
			InitializeComponent();
			BindingContext = _viewModel = new ArtistsViewModel();
		}

		protected override void OnAppearing()
		{
		  base.OnAppearing();
		  if (_viewModel.IsInitialized)
			return;

		  _viewModel.IsInitialized = true;
		  _viewModel.LoadCommand.Execute(null);
		}
	}

	public class BaseViewModel : INotifyPropertyChanged
	{
		public string Title { get; set; }
		public bool IsInitialized { get; set; }

		bool _isBusy;

		/// <summary>
		/// Gets or sets if VM is busy working
		/// </summary>
		public bool IsBusy
		{
			get { return _isBusy; }
			set { _isBusy = value; OnPropertyChanged("IsBusy"); }
		}

		//INotifyPropertyChanged Implementation
		public event PropertyChangedEventHandler PropertyChanged;

		protected void OnPropertyChanged(string propertyName)
		{
			if (PropertyChanged == null)
			return;

			PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
		}
	}

	public class ArtistsViewModel : BaseViewModel
	{
		public ArtistsViewModel()
		{
			Title = "Artists";
			Artists = new ObservableCollection<Artist>();
		}

		/// <summary>
		/// gets or sets the feed items
		/// </summary>
		public ObservableCollection<Artist> Artists
		{
			get;
			private set;
		}

		Command _loadCommand;
		/// <summary>
		/// Command to load/refresh artitists
		/// </summary>
		public Command LoadCommand
		{
			get { return _loadCommand ?? (_loadCommand = new Command(async () => await ExecuteLoadCommand())); }
		}

		static readonly Artist[] ArtistsToLoad = new Artist[] {
			new Artist { Name = "Metallica", ListenerCount = "100", PlayCount = "5000" },
			new Artist { Name = "Epica", ListenerCount = "50", PlayCount = "1000" }
		};

		async Task ExecuteLoadCommand()
		{
			if (IsBusy)
				return;

			IsBusy = true;

			Artists.Clear();

			await Task.Delay (3000);

			foreach (Artist a in ArtistsToLoad)
				Artists.Add (a);

			IsBusy = false;
		}
	}

	public class Artist
	{

		public string Name { get; set; }

		public string PlayCount { get; set; }

		public string ListenerCount { get; set; }

		public string Mbid { get; set; }

		public string Url { get; set; }

		public string Streamable { get; set; }
	}
#endif
}