个人资料

跳过导航链接首页 > 博客列表 > 博客正文

C# 使用 XboxLive 用户登录/资料

分类:

xbox用户登录


        private static async Task<bool> SignInResult()
        {

            IReadOnlyList<User> users = await User.FindAllAsync();
            user = users[0];
            xboxUser = new XboxLiveUser(user);
            Debug.WriteLine("开始登入 - sign in" + user.AuthenticationStatus.ToString());
            var r = await xboxUser.SignInSilentlyAsync(Window.Current.CoreWindow.Dispatcher);

            switch (r.Status)
            {
                case SignInStatus.Success:
                    
                    Debug.WriteLine("成功登入xbox");

                    return true;///////////////////////////

                case SignInStatus.UserInteractionRequired:
                    Debug.WriteLine("xbox登入需要用户参与");
                    try
                    {
                        var rr = await xboxUser.SignInAsync(null);
                        switch (rr.Status)
                        {
                            case SignInStatus.Success:
                                Debug.WriteLine("成功登入xbox");
                                return true;
                            case SignInStatus.UserInteractionRequired:
                                Debug.WriteLine("xbox登入需要用户参与_2");
                                break;
                            case SignInStatus.UserCancel:
                                Debug.WriteLine("用户取消xbox登入_2");
                                break;
                            default:
                                break;
                        }
                    }
                    catch (Exception ex)
                    {

                        Debug.WriteLine("再次登录出错"+ex.Message);
                    }
                    

                    break;
                case SignInStatus.UserCancel:
                    Debug.WriteLine("用户取消xbox登入");
                    break;
                default:
                    break;
            }


            return false;

        }


获取用户资料


        public async static Task<List<XboxUserProfile>> GetUserProfilesAsync(List<string> id_list) {

            if (!is_online) return null;

            var profiles = await context.ProfileService.GetUserProfilesAsync(id_list);
            
            
            return profiles.ToList();

        }


检测是否在线


        public static bool CheckOnline() {

            //Retrieve the ConnectionProfile
            string connectionProfileInfo = string.Empty;
            ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
            NetworkConnectivityLevel connectionLevel = InternetConnectionProfile.GetNetworkConnectivityLevel();
            switch (connectionLevel)
            {
                case NetworkConnectivityLevel.InternetAccess:
                    // User is connected to the internet.
                    return true;
                case NetworkConnectivityLevel.ConstrainedInternetAccess: 
                    //Limited Internet Access PossibleAuthentication Required
                    // display error message for user.
                    Debug.WriteLine("Game Offline: Limited internet access, browser authentication may be required. "); //function writes to UI
            
                    break;
                default:
                    Debug.WriteLine("Game Offline: No internet access.");
                    break;
            }

            return false;

        }




 public static async void XboxLiveSignIn()
        {
            if (is_online) return;


            if (await SignInResult())
            {
                context = new XboxLiveContext(xboxUser);
                Debug.WriteLine("添加统计用户");
                StatisticManager.SingletonInstance.AddLocalUser(xboxUser);
                Debug.WriteLine("获取用户信息");
                profile =await context.ProfileService.GetUserProfileAsync(xboxUser.XboxUserId);
                Debug.WriteLine("通知用户状态");
                MainPage.PopAction?.Invoke(xboxUser.Gamertag, AppResources.GetString("xbox_in_text"));
                Debug.WriteLine("更新用户资料");
                MenuPage.UpdateProfile?.Invoke(profile);
            }
            else
            {

                MainPage.PopAction?.Invoke(user.NonRoamableId, AppResources.GetString("xbox_out_text"));
                
            }
            XboxLiveUser.SignOutCompleted += (s,e)=> {
                user = null;
                context = null;
            };

        }


~

songshizhao
最初发表2022/5/1 23:56:35 最近更新2022/5/2 0:05:20 30
为此篇作品打分
10
   评论