PageLoginMs.xaml.vb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. Public Class PageLoginMs
  2. ''' <summary>
  3. ''' 刷新页面显示的所有信息。
  4. ''' </summary>
  5. Public Sub Reload(KeepInput As Boolean)
  6. Dim IndexBefore = ComboAccounts.SelectedIndex
  7. '刷新下拉框列表
  8. ComboAccounts.Items.Clear()
  9. ComboAccounts.Items.Add(New MyComboBoxItem With {.Content = "添加新账号"})
  10. Try
  11. Dim MsJson As JObject = GetJson(Setup.Get("LoginMsJson"))
  12. For Each Account In MsJson
  13. Dim Item As MyListItem = CType(FindResource("ComboBoxItemTemplateWithDelete"), DataTemplate).LoadContent()
  14. Item.Tag = Account.Value.ToString
  15. Item.Title = Account.Key
  16. CType(Item.Buttons(0), MyIconButton).Tag = Account.Key
  17. ComboAccounts.Items.Add(Item)
  18. Next
  19. Catch ex As Exception
  20. Log(ex, $"微软登录信息出错,登录信息已被重置({Setup.Get("LoginMsJson")})", LogLevel.Hint)
  21. Setup.Set("LoginMsJson", "{}")
  22. End Try
  23. '如果不保留输入,刷新列表后自动选择第一项
  24. ComboAccounts.SelectedIndex = If(KeepInput, Math.Max(0, IndexBefore), 0)
  25. End Sub
  26. Private Sub ComboAccounts_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles ComboAccounts.SelectionChanged
  27. If AniControlEnabled <> 0 OrElse ComboAccounts.SelectedItem Is Nothing OrElse ComboAccounts.ContentPresenter Is Nothing Then Exit Sub
  28. If TypeOf ComboAccounts.SelectedItem Is MyListItem Then
  29. ComboAccounts.ContentPresenter.Content = CType(ComboAccounts.SelectedItem, MyListItem).Title
  30. ElseIf TypeOf ComboAccounts.SelectedItem Is MyComboBoxItem Then
  31. ComboAccounts.ContentPresenter.Content = CType(ComboAccounts.SelectedItem, MyComboBoxItem).Content
  32. End If
  33. End Sub
  34. ''' <summary>
  35. ''' 获取当前页面的登录信息。
  36. ''' </summary>
  37. Public Shared Function GetLoginData() As McLoginMs
  38. If FrmLoginMs Is Nothing Then Return New McLoginMs With {.OAuthRefreshToken = Setup.Get("CacheMsOAuthRefresh"), .UserName = Setup.Get("CacheMsName")}
  39. Dim Result As McLoginMs = Nothing
  40. RunInUiWait(Sub()
  41. If FrmLoginMs.ComboAccounts.SelectedIndex = 0 Then
  42. Result = New McLoginMs
  43. Else
  44. Dim Item As MyListItem = FrmLoginMs.ComboAccounts.SelectedItem
  45. Result = New McLoginMs With {.OAuthRefreshToken = Item.Tag, .UserName = Item.Title}
  46. End If
  47. End Sub)
  48. Return Result
  49. End Function
  50. ''' <summary>
  51. ''' 当前页面的登录信息是否有效。
  52. ''' </summary>
  53. Public Shared Function IsVaild(LoginData As McLoginMs) As String
  54. If LoginData.OAuthRefreshToken = "" Then
  55. Return "请在登录账号后再启动游戏!"
  56. Else
  57. Return ""
  58. End If
  59. End Function
  60. Public Function IsVaild() As String
  61. Return IsVaild(GetLoginData())
  62. End Function
  63. Private Sub BtnLogin_Click(sender As Object, e As EventArgs) Handles BtnLogin.Click
  64. ComboAccounts.IsEnabled = False
  65. BtnLogin.IsEnabled = False
  66. BtnLogin.Text = "0%"
  67. RunInNewThread(Sub()
  68. Try
  69. McLoginMsLoader.Start(GetLoginData(), IsForceRestart:=True)
  70. Do While McLoginMsLoader.State = LoadState.Loading
  71. RunInUi(Sub() BtnLogin.Text = Math.Round(McLoginMsLoader.Progress * 100) & "%")
  72. Thread.Sleep(50)
  73. Loop
  74. If McLoginMsLoader.State = LoadState.Finished Then
  75. RunInUi(Sub() FrmLaunchLeft.RefreshPage(False, True))
  76. ElseIf McLoginMsLoader.State = LoadState.Aborted Then
  77. Throw New ThreadInterruptedException
  78. ElseIf McLoginMsLoader.Error Is Nothing Then
  79. Throw New Exception("未知错误!")
  80. Else
  81. Throw New Exception(McLoginMsLoader.Error.Message, McLoginMsLoader.Error)
  82. End If
  83. Catch ex As ThreadInterruptedException
  84. Hint("已取消登录!")
  85. Catch ex As Exception
  86. If ex.Message = "$$" Then
  87. ElseIf ex.Message.StartsWith("$") Then
  88. Hint(ex.Message.TrimStart("$"), HintType.Critical)
  89. Else
  90. Log(ex, "微软登录尝试失败", LogLevel.Msgbox)
  91. End If
  92. Finally
  93. RunInUi(Sub()
  94. ComboAccounts.IsEnabled = True
  95. BtnLogin.IsEnabled = True
  96. BtnLogin.Text = "登录"
  97. End Sub)
  98. End Try
  99. End Sub, "Ms Login")
  100. End Sub
  101. End Class