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. using System.Threading;
  8. namespace Egametang
  9. {
  10. public class MainViewModel : ViewModelBase
  11. {
  12. private readonly IDataService dataService;
  13. private string loginResult = "";
  14. public MainViewModel(IDataService dataService)
  15. {
  16. this.dataService = dataService;
  17. LoginCmd = new RelayCommand(AsyncLogin);
  18. }
  19. public string LoginResult
  20. {
  21. get
  22. {
  23. return loginResult;
  24. }
  25. set
  26. {
  27. if (loginResult == value)
  28. {
  29. return;
  30. }
  31. loginResult = value;
  32. RaisePropertyChanged("LoginResult");
  33. }
  34. }
  35. public RelayCommand LoginCmd
  36. {
  37. get;
  38. private set;
  39. }
  40. private void AsyncLogin()
  41. {
  42. var task = new Task(() =>
  43. {
  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. }