MainViewModel.cs 1.2 KB

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