LoginViewModel.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 Logger;
  9. using Microsoft.Practices.Prism.Mvvm;
  10. using Microsoft.Practices.Prism.PubSubEvents;
  11. namespace Login
  12. {
  13. [Export(contractType: typeof (LoginViewModel)),
  14. PartCreationPolicy(creationPolicy: CreationPolicy.NonShared)]
  15. internal class LoginViewModel: BindableBase
  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. {
  25. Interval = new TimeSpan(0, 0, 0, 0, 50)
  26. };
  27. public Visibility LoginWindowVisiable
  28. {
  29. get
  30. {
  31. return this.loginWindowVisiable;
  32. }
  33. set
  34. {
  35. this.SetProperty(ref this.loginWindowVisiable, value);
  36. }
  37. }
  38. public string Account
  39. {
  40. get
  41. {
  42. return this.account;
  43. }
  44. set
  45. {
  46. this.SetProperty(ref this.account, value);
  47. }
  48. }
  49. public string Password
  50. {
  51. get
  52. {
  53. return this.password;
  54. }
  55. set
  56. {
  57. this.SetProperty(ref this.password, value);
  58. }
  59. }
  60. public string ErrorInfo
  61. {
  62. get
  63. {
  64. return this.errorInfo;
  65. }
  66. set
  67. {
  68. this.SetProperty(ref this.errorInfo, value);
  69. }
  70. }
  71. [ImportingConstructor]
  72. public LoginViewModel(IEventAggregator eventAggregator)
  73. {
  74. this.EventAggregator = eventAggregator;
  75. this.EventAggregator.GetEvent<ReLoginEvent>().Subscribe(this.OnReLoginEvent);
  76. this.timer.Tick += delegate { this.bossClient.RunOnce(); };
  77. this.timer.Start();
  78. }
  79. public void OnReLoginEvent(object obj)
  80. {
  81. this.LoginWindowVisiable = Visibility.Visible;
  82. }
  83. public async Task Login()
  84. {
  85. string ip = ConfigurationManager.AppSettings["IP"];
  86. ushort port = UInt16.Parse(ConfigurationManager.AppSettings["Port"]);
  87. if (this.Account == "")
  88. {
  89. this.Account = ConfigurationManager.AppSettings["Account"];
  90. }
  91. if (this.Password == "")
  92. {
  93. this.Password = ConfigurationManager.AppSettings["Password"];
  94. }
  95. try
  96. {
  97. await this.bossClient.Login(ip, port, this.Account, this.Password);
  98. }
  99. catch (Exception e)
  100. {
  101. this.ErrorInfo = "登录失败";
  102. Log.Trace(e.ToString());
  103. return;
  104. }
  105. this.LoginWindowVisiable = Visibility.Hidden;
  106. this.EventAggregator.GetEvent<LoginOKEvent>()
  107. .Publish(this.bossClient.GateSession.IMessageChannel);
  108. }
  109. }
  110. }