如何将用户信息从登录页面传递到主页? [英] how can i pass user information from loginpage to HomePage?

查看:44
本文介绍了如何将用户信息从登录页面传递到主页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

错误非静态字段、方法或属性User.Picture"需要对象引用

如何将用户信息从登录页面传递到主页?在线错误 PictureImage.Source = User.Picture;

HomePage.xaml

公共部分类 HomePage : ContentPage{公共主页(){初始化组件();PictureImage.Source = User.Picture;IdLabel.Text = 用户 ID;NameLabel.Text = User.Name;....}}

LoginPage.xaml.cs 在这里用户可以使用谷歌电子邮件和密码登录.如果没有错误,则运行 OnAuthCompleted 方法.在这种方法中,我正在获取用户信息并转到主页.

async void OnAuthCompleted(object sender, AuthenticatorCompletedEventArgs e){var authenticationator = sender as OAuth2Authenticator;如果(身份验证器!= null){authenticationator.Completed -= OnAuthCompleted;验证器.Error -= OnAuthError;}用户 user = null;if (e.IsAuthenticated){//如果用户通过身份验证,从谷歌请求他们的基本用户数据//UserInfoUrl = https://www.googleapis.com/oauth2/v2/userinfovar request = new OAuth2Request(GET", new Uri(Constants.UserInfoUrl), null, e.Account);var response = await request.GetResponseAsync();如果(响应!= null){//反序列化数据并将其存储在帐户存储中//用户电子邮件地址将用于识别 SimpleDB 中的数据string userJson = await response.GetResponseTextAsync();user = JsonConvert.DeserializeObject(userJson);}如果(用户!= null){var route = $"{nameof(HomePage)}";等待 Shell.Current.GoToAsync(route);//App.Current.MainPage = new NavigationPage(new HomePage());}await store.SaveAsync(account = e.Account, Constants.AppName);await DisplayAlert("Email address", user.Email, "OK");}}//方法结束

用户类这里是为用户信息创建getter和setter

 [JsonObject]公共类用户{[JsonProperty(id")]公共字符串 ID { 获取;放;}[JsonProperty(电子邮件")]公共字符串电子邮件{获取;放;}[JsonProperty(verified_email")]public bool VerifiedEmail { get;放;}[JsonProperty("name")]公共字符串名称{获取;放;}[JsonProperty(given_name")]公共字符串 GivenName { get;放;}[JsonProperty("family_name")]公共字符串 FamilyName { 获取;放;}[JsonProperty(链接")]公共字符串链接{获取;放;}[JsonProperty(图片")]公共字符串图片{获取;放;}[JsonProperty(性别")]公共字符串性别{获取;放;}}

AuthenticationState 类

公共类AuthenticationState{公共静态 OAuth2Authenticator 身份验证器;}

解决方案

User 属性添加到您的 App

public User CurrentUser { get;放;}

然后你可以在你的应用程序的任何地方访问它

if (App.Current.CurrentUser == null){App.Current.CurrentUser = 新用户 { ... };}

Error An object reference is required for the non-static field, method, or property 'User.Picture'

How can I pass user information from loginpage to HomePage? error on line PictureImage.Source = User.Picture;

HomePage.xaml

public partial class HomePage : ContentPage
    {
        public HomePage()
        {
            InitializeComponent();
             
            PictureImage.Source = User.Picture;
             IdLabel.Text = User.ID;
           NameLabel.Text = User.Name;
            ....
         }
}

LoginPage.xaml.cs here user can login using there google email and password. if no errors than run OnAuthCompleted method. In this method i am getting user information and going to homepage.

async void OnAuthCompleted(object sender, AuthenticatorCompletedEventArgs e)
{
    var authenticator = sender as OAuth2Authenticator;
    if (authenticator != null)
    {
        authenticator.Completed -= OnAuthCompleted;
        authenticator.Error -= OnAuthError;
    }

    User user = null;
    if (e.IsAuthenticated)
    {
        // If the user is authenticated, request their basic user data from Google
        // UserInfoUrl = https://www.googleapis.com/oauth2/v2/userinfo
        var request = new OAuth2Request("GET", new Uri(Constants.UserInfoUrl), null, e.Account);
        var response = await request.GetResponseAsync();
        if (response != null)
        {
            // Deserialize the data and store it in the account store
            // The users email address will be used to identify data in SimpleDB
            string userJson = await response.GetResponseTextAsync();
            user = JsonConvert.DeserializeObject<User>(userJson);
        }

        if (user != null)
        {
            var route = $"{ nameof(HomePage)}";
            await Shell.Current.GoToAsync(route);
            //App.Current.MainPage = new NavigationPage(new HomePage());
        }

        await store.SaveAsync(account = e.Account, Constants.AppName);
        await DisplayAlert("Email address", user.Email, "OK");
    }
}//end of method

user class here ia m creating getter and setter for user information

    [JsonObject]
    public class User
    {
         [JsonProperty("id")]
         public string Id { get; set; }

        [JsonProperty("email")]
        public string Email { get; set; }

        [JsonProperty("verified_email")]
        public bool VerifiedEmail { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("given_name")]
        public string GivenName { get; set; }

        [JsonProperty("family_name")]
        public string FamilyName { get; set; }

        [JsonProperty("link")]
        public string Link { get; set; }

        [JsonProperty("picture")]
        public string Picture { get; set; }

        [JsonProperty("gender")]
        public string Gender { get; set; }
    }

AuthenticationState class

public class AuthenticationState
{
    public static OAuth2Authenticator Authenticator;
}

解决方案

add a User property to your App class

public User CurrentUser { get; set; }

then anywhere in your app you can access it

if (App.Current.CurrentUser == null) 
{
    App.Current.CurrentUser = new User { ... };
}

这篇关于如何将用户信息从登录页面传递到主页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆