-
Notifications
You must be signed in to change notification settings - Fork 15
/
ViewModel.cs
89 lines (77 loc) · 2.55 KB
/
ViewModel.cs
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
using System.Windows.Input;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace LoadMore {
public class ViewModel : INotifyPropertyChanged {
OrderData data;
public bool isRefreshing = false;
public bool IsRefreshing {
get { return isRefreshing; }
set {
if (isRefreshing != value) {
isRefreshing = value;
OnPropertyChanged("IsRefreshing");
}
}
}
ObservableCollection<Order> orders;
public ObservableCollection<Order> Orders {
get { return orders; }
set {
if (orders != value) {
orders = value;
OnPropertyChanged("Products");
}
}
}
LoadMoreDataCommand loadMoreCommand = null;
public LoadMoreDataCommand LoadMoreCommand {
get { return loadMoreCommand; }
set {
if (loadMoreCommand != value) {
loadMoreCommand = value;
OnPropertyChanged("LoadMoreCommand");
}
}
}
public ViewModel() {
this.data = new OrderData();
Orders = data.Orders;
LoadMoreCommand = new LoadMoreDataCommand(ExecuteLoadMoreCommand);
}
void ExecuteLoadMoreCommand() {
Task.Run(() => {
Thread.Sleep(1000);
Device.BeginInvokeOnMainThread(() => {
data.LoadMoreOrders();
Orders = data.Orders;
IsRefreshing = false;
});
});
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name) {
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
public class LoadMoreDataCommand : ICommand {
readonly Action execute;
int numOfLoadMore = 0;
public event EventHandler CanExecuteChanged;
public LoadMoreDataCommand(Action execute) {
this.execute = execute;
}
public bool CanExecute(object parameter) {
return numOfLoadMore < 3;
}
public void Execute(object parameter) {
numOfLoadMore++;
ChangeCanExecute();
this.execute();
}
void ChangeCanExecute() {
CanExecuteChanged?.Invoke(this, new EventArgs());
}
}
}