LoginViewModel.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 Events;
  8. using Microsoft.Practices.Prism.Events;
  9. using Microsoft.Practices.Prism.ViewModel;
  10. namespace Modules.Login
  11. {
  12. [Export(contractType: typeof (LoginViewModel)),
  13. PartCreationPolicy(creationPolicy: CreationPolicy.NonShared)]
  14. internal class LoginViewModel : NotificationObject
  15. {
  16. private IEventAggregator EventAggregator { get; set; }
  17. private string account = "egametang@126.com";
  18. private string password = "163bio1";
  19. private Visibility loginWindowVisiable = Visibility.Visible;
  20. private readonly BossClient.BossClient bossClient = new BossClient.BossClient();
  21. private readonly DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Normal)
  22. { Interval = new TimeSpan(0, 0, 0, 0, 50) };
  23. public Visibility LoginWindowVisiable
  24. {
  25. get
  26. {
  27. return this.loginWindowVisiable;
  28. }
  29. set
  30. {
  31. if (this.loginWindowVisiable == value)
  32. {
  33. return;
  34. }
  35. this.loginWindowVisiable = value;
  36. this.RaisePropertyChanged("LoginWindowVisiable");
  37. }
  38. }
  39. public string Account
  40. {
  41. get
  42. {
  43. return this.account;
  44. }
  45. set
  46. {
  47. if (this.account == value)
  48. {
  49. return;
  50. }
  51. this.account = value;
  52. this.RaisePropertyChanged("Account");
  53. }
  54. }
  55. public string Password
  56. {
  57. get
  58. {
  59. return this.password;
  60. }
  61. set
  62. {
  63. if (this.password == value)
  64. {
  65. return;
  66. }
  67. this.password = value;
  68. this.RaisePropertyChanged("Password");
  69. }
  70. }
  71. [ImportingConstructor]
  72. public LoginViewModel(IEventAggregator eventAggregator)
  73. {
  74. this.EventAggregator = eventAggregator;
  75. this.timer.Tick += delegate { this.bossClient.RunOnce(); };
  76. this.timer.Start();
  77. }
  78. public async Task Login()
  79. {
  80. string ip = ConfigurationManager.AppSettings["IP"];
  81. ushort port = UInt16.Parse(ConfigurationManager.AppSettings["Port"]);
  82. await this.bossClient.Login(ip, port, this.Account, this.Password);
  83. this.LoginWindowVisiable = Visibility.Hidden;
  84. this.EventAggregator.GetEvent<LoginOKEvent>().Publish(
  85. bossClient.GateSession.IMessageChannel);
  86. }
  87. }
  88. }