LoginViewModel.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.ComponentModel.Composition;
  3. using System.Configuration;
  4. using System.Threading.Tasks;
  5. using System.Windows;
  6. using System.Windows.Threading;
  7. using BossBase;
  8. using Log;
  9. using Microsoft.Practices.Prism.Events;
  10. using Microsoft.Practices.Prism.ViewModel;
  11. namespace Login
  12. {
  13. [Export(contractType: typeof (LoginViewModel)),
  14. PartCreationPolicy(creationPolicy: CreationPolicy.NonShared)]
  15. internal class LoginViewModel : NotificationObject
  16. {
  17. private IEventAggregator EventAggregator { get; set; }
  18. private string account = "";
  19. private string password = "";
  20. private string errorInfo = "";
  21. private Visibility loginWindowVisiable = Visibility.Visible;
  22. private readonly BossClient.BossClient bossClient = new BossClient.BossClient();
  23. private readonly DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Normal)
  24. { Interval = new TimeSpan(0, 0, 0, 0, 50) };
  25. public Visibility LoginWindowVisiable
  26. {
  27. get
  28. {
  29. return this.loginWindowVisiable;
  30. }
  31. set
  32. {
  33. if (this.loginWindowVisiable == value)
  34. {
  35. return;
  36. }
  37. this.loginWindowVisiable = value;
  38. this.RaisePropertyChanged("LoginWindowVisiable");
  39. }
  40. }
  41. public string Account
  42. {
  43. get
  44. {
  45. return this.account;
  46. }
  47. set
  48. {
  49. if (this.account == value)
  50. {
  51. return;
  52. }
  53. this.account = value;
  54. this.RaisePropertyChanged("Account");
  55. }
  56. }
  57. public string Password
  58. {
  59. get
  60. {
  61. return this.password;
  62. }
  63. set
  64. {
  65. if (this.password == value)
  66. {
  67. return;
  68. }
  69. this.password = value;
  70. this.RaisePropertyChanged("Password");
  71. }
  72. }
  73. public string ErrorInfo
  74. {
  75. get
  76. {
  77. return this.errorInfo;
  78. }
  79. set
  80. {
  81. if (this.errorInfo == value)
  82. {
  83. return;
  84. }
  85. this.errorInfo = value;
  86. this.RaisePropertyChanged("ErrorInfo");
  87. }
  88. }
  89. [ImportingConstructor]
  90. public LoginViewModel(IEventAggregator eventAggregator)
  91. {
  92. this.EventAggregator = eventAggregator;
  93. this.EventAggregator.GetEvent<ReLoginEvent>().Subscribe(this.OnReLoginEvent);
  94. this.timer.Tick += delegate { this.bossClient.RunOnce(); };
  95. this.timer.Start();
  96. }
  97. public void OnReLoginEvent(object obj)
  98. {
  99. this.LoginWindowVisiable = Visibility.Visible;
  100. }
  101. public async Task Login()
  102. {
  103. string ip = ConfigurationManager.AppSettings["IP"];
  104. ushort port = UInt16.Parse(ConfigurationManager.AppSettings["Port"]);
  105. if (this.Account == "")
  106. {
  107. this.Account = ConfigurationManager.AppSettings["Account"];
  108. }
  109. if (this.Password == "")
  110. {
  111. this.Password = ConfigurationManager.AppSettings["Password"];
  112. }
  113. try
  114. {
  115. await this.bossClient.Login(ip, port, this.Account, this.Password);
  116. }
  117. catch (Exception e)
  118. {
  119. this.ErrorInfo = "登录失败";
  120. Logger.Trace(e.ToString());
  121. return;
  122. }
  123. this.LoginWindowVisiable = Visibility.Hidden;
  124. this.EventAggregator.GetEvent<LoginOKEvent>().Publish(
  125. bossClient.GateSession.IMessageChannel);
  126. }
  127. }
  128. }