Public Class PageLoginMs ''' ''' 刷新页面显示的所有信息。 ''' Public Sub Reload(KeepInput As Boolean) Dim IndexBefore = ComboAccounts.SelectedIndex '刷新下拉框列表 ComboAccounts.Items.Clear() ComboAccounts.Items.Add(New MyComboBoxItem With {.Content = "添加新账号"}) Try Dim MsJson As JObject = GetJson(Setup.Get("LoginMsJson")) For Each Account In MsJson Dim Item As MyListItem = CType(FindResource("ComboBoxItemTemplateWithDelete"), DataTemplate).LoadContent() Item.Tag = Account.Value.ToString Item.Title = Account.Key CType(Item.Buttons(0), MyIconButton).Tag = Account.Key ComboAccounts.Items.Add(Item) Next Catch ex As Exception Log(ex, $"微软登录信息出错,登录信息已被重置({Setup.Get("LoginMsJson")})", LogLevel.Hint) Setup.Set("LoginMsJson", "{}") End Try '如果不保留输入,刷新列表后自动选择第一项 ComboAccounts.SelectedIndex = If(KeepInput, Math.Max(0, IndexBefore), 0) End Sub Private Sub ComboAccounts_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles ComboAccounts.SelectionChanged If AniControlEnabled <> 0 OrElse ComboAccounts.SelectedItem Is Nothing Then Exit Sub If TypeOf ComboAccounts.SelectedItem Is MyListItem Then ComboAccounts.ContentPresenter.Content = CType(ComboAccounts.SelectedItem, MyListItem).Title ElseIf TypeOf ComboAccounts.SelectedItem Is MyComboBoxItem Then ComboAccounts.ContentPresenter.Content = CType(ComboAccounts.SelectedItem, MyComboBoxItem).Content End If End Sub ''' ''' 获取当前页面的登录信息。 ''' Public Shared Function GetLoginData() As McLoginMs If FrmLoginMs Is Nothing Then Return New McLoginMs With {.OAuthRefreshToken = Setup.Get("CacheMsOAuthRefresh"), .UserName = Setup.Get("CacheMsName")} Dim Result As McLoginMs = Nothing RunInUiWait(Sub() If FrmLoginMs.ComboAccounts.SelectedIndex = 0 Then Result = New McLoginMs Else Dim Item As MyListItem = FrmLoginMs.ComboAccounts.SelectedItem Result = New McLoginMs With {.OAuthRefreshToken = Item.Tag, .UserName = Item.Title} End If End Sub) Return Result End Function ''' ''' 当前页面的登录信息是否有效。 ''' Public Shared Function IsVaild(LoginData As McLoginMs) As String If LoginData.OAuthRefreshToken = "" Then Return "请在登录账号后再启动游戏!" Else Return "" End If End Function Public Function IsVaild() As String Return IsVaild(GetLoginData()) End Function Private Sub BtnLogin_Click(sender As Object, e As EventArgs) Handles BtnLogin.Click ComboAccounts.IsEnabled = False BtnLogin.IsEnabled = False BtnLogin.Text = "0%" RunInNewThread(Sub() Try McLoginMsLoader.Start(GetLoginData(), IsForceRestart:=True) Do While McLoginMsLoader.State = LoadState.Loading RunInUi(Sub() BtnLogin.Text = Math.Round(McLoginMsLoader.Progress * 100) & "%") Thread.Sleep(50) Loop If McLoginMsLoader.State = LoadState.Finished Then RunInUi(Sub() FrmLaunchLeft.RefreshPage(False, True)) ElseIf McLoginMsLoader.State = LoadState.Aborted Then Throw New ThreadInterruptedException ElseIf McLoginMsLoader.Error Is Nothing Then Throw New Exception("未知错误!") Else Throw New Exception(McLoginMsLoader.Error.Message, McLoginMsLoader.Error) End If Catch ex As ThreadInterruptedException Hint("已取消登录!") Catch ex As Exception If ex.Message = "$$" Then ElseIf ex.Message.StartsWith("$") Then Hint(ex.Message.TrimStart("$"), HintType.Critical) Else Log(ex, "微软登录尝试失败", LogLevel.Msgbox) End If Finally RunInUi(Sub() ComboAccounts.IsEnabled = True BtnLogin.IsEnabled = True BtnLogin.Text = "登录" End Sub) End Try End Sub, "Ms Login") End Sub '账号迁移入口 Private Sub BtnMigration_Click(sender As Object, e As EventArgs) Handles BtnMigration.Click MyMsgBox("在迁移过程中,你可能需要设置你的档案信息,此时请注意让年龄大于 18 岁,否则可能导致无法登录!", "迁移提示", "继续", ForceWait:=True) OpenWebsite("https://www.minecraft.net/zh-hans/account-security") End Sub End Class