-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainViewModel.cs
51 lines (47 loc) · 1.68 KB
/
MainViewModel.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
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm.Xpf;
using EntityFrameworkIssues.Issues;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AsyncCRUDOperations {
public class MainViewModel : ViewModelBase {
IssuesContext _Context;
IList<User> _ItemsSource;
public IList<User> ItemsSource { get { return _ItemsSource; } }
[Command]
public void ValidateRow(RowValidationArgs args) {
var item = (User)args.Item;
args.ResultAsync = Task.Run(async () => {
await Task.Delay(3000);
if(args.IsNewItem) {
_Context.Users.Add(item);
} else {
_Context.SaveChanges();
}
return new ValidationErrorInfo(null);
});
}
[Command]
public void ValidateRowDeletion(ValidateRowDeletionArgs args) {
var item = (User)args.Items.Single();
args.ResultAsync = Task.Run(async () => {
await Task.Delay(3000);
_Context.Users.Remove(item);
_Context.SaveChanges();
return new ValidationErrorInfo(null);
});
}
[Command]
public void DataSourceRefresh(DataSourceRefreshArgs args) {
args.RefreshAsync = RefreshDataSoruceAsync();
}
async Task RefreshDataSoruceAsync() {
await Task.Delay(3000).ConfigureAwait(false);
_Context = new IssuesContext();
_ItemsSource = _Context.Users.ToList();
RaisePropertyChanged(nameof(ItemsSource));
}
}
}