LoginViewModel.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. { 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. this.SetProperty(ref this.loginWindowVisiable, value);
  34. }
  35. }
  36. public string Account
  37. {
  38. get
  39. {
  40. return this.account;
  41. }
  42. set
  43. {
  44. this.SetProperty(ref this.account, value);
  45. }
  46. }
  47. public string Password
  48. {
  49. get
  50. {
  51. return this.password;
  52. }
  53. set
  54. {
  55. this.SetProperty(ref this.password, value);
  56. }
  57. }
  58. public string ErrorInfo
  59. {
  60. get
  61. {
  62. return this.errorInfo;
  63. }
  64. set
  65. {
  66. this.SetProperty(ref this.errorInfo, value);
  67. }
  68. }
  69. [ImportingConstructor]
  70. public LoginViewModel(IEventAggregator eventAggregator)
  71. {
  72. this.EventAggregator = eventAggregator;
  73. this.EventAggregator.GetEvent<ReLoginEvent>().Subscribe(this.OnReLoginEvent);
  74. this.timer.Tick += delegate { this.bossClient.RunOnce(); };
  75. this.timer.Start();
  76. }
  77. public void OnReLoginEvent(object obj)
  78. {
  79. this.LoginWindowVisiable = Visibility.Visible;
  80. }
  81. public async Task Login()
  82. {
  83. string ip = ConfigurationManager.AppSettings["IP"];
  84. ushort port = UInt16.Parse(ConfigurationManager.AppSettings["Port"]);
  85. if (this.Account == "")
  86. {
  87. this.Account = ConfigurationManager.AppSettings["Account"];
  88. }
  89. if (this.Password == "")
  90. {
  91. this.Password = ConfigurationManager.AppSettings["Password"];
  92. }
  93. try
  94. {
  95. await this.bossClient.Login(ip, port, this.Account, this.Password);
  96. }
  97. catch (Exception e)
  98. {
  99. this.ErrorInfo = "登录失败";
  100. Log.Trace(e.ToString());
  101. return;
  102. }
  103. this.LoginWindowVisiable = Visibility.Hidden;
  104. this.EventAggregator.GetEvent<LoginOKEvent>().Publish(
  105. bossClient.GateSession.IMessageChannel);
  106. }
  107. }
  108. }