MainViewModel.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Threading.Tasks.Schedulers;
  4. using System.Windows.Threading;
  5. using GalaSoft.MvvmLight;
  6. using GalaSoft.MvvmLight.Command;
  7. using GalaSoft.MvvmLight.Threading;
  8. namespace Egametang
  9. {
  10. public class MainViewModel : ViewModelBase
  11. {
  12. private readonly IDataService dataService;
  13. private string loginResult = "";
  14. private OrderedTaskScheduler orderedTaskScheduler = new OrderedTaskScheduler();
  15. public MainViewModel(IDataService dataService)
  16. {
  17. this.dataService = dataService;
  18. LoginCmd = new RelayCommand(AsyncLogin);
  19. }
  20. public string LoginResult
  21. {
  22. get
  23. {
  24. return loginResult;
  25. }
  26. set
  27. {
  28. if (loginResult == value)
  29. {
  30. return;
  31. }
  32. loginResult = value;
  33. RaisePropertyChanged("LoginResult");
  34. }
  35. }
  36. public RelayCommand LoginCmd
  37. {
  38. get;
  39. private set;
  40. }
  41. private void AsyncLogin()
  42. {
  43. var task = new Task(() =>
  44. {
  45. });
  46. task.ContinueWith(_ =>
  47. {
  48. DispatcherHelper.UIDispatcher.BeginInvoke(DispatcherPriority.Normal,
  49. new Action(() =>
  50. {
  51. LoginResult = "Login OK!";
  52. }));
  53. }, orderedTaskScheduler);
  54. task.Start();
  55. }
  56. public override void Cleanup()
  57. {
  58. base.Cleanup();
  59. }
  60. }
  61. }