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. App.Logger.Debug("11111");
  44. });
  45. task.ContinueWith(_ =>
  46. {
  47. DispatcherHelper.UIDispatcher.BeginInvoke(DispatcherPriority.Normal,
  48. new Action(() =>
  49. {
  50. LoginResult = "Login OK!";
  51. }));
  52. });
  53. task.Start(App.OrderedTaskScheduler);
  54. }
  55. public override void Cleanup()
  56. {
  57. base.Cleanup();
  58. }
  59. }
  60. }