Xamarin在登录成功时表单导航

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (283)

我开发了一个使用MS Identitity的WebApi项目,并将数据提供给Html / Jquery客户端。我第一次参加Xamarin表格并按照教程进行操作。我已经到了一个阶段,我可以成功发布登录标准并收到持票人令牌,并使用Xam Plugins.Settings存储它。我可以在将来的请求中使用此令牌并接收数据。我正在努力实现导航,所以当用户点击登录按钮并发出令牌时,应用程序将“重定向”到视图GSMUnitsPage()。目前用户点击按钮并登录正常,但它位于同一个登录屏幕上....我已经通过创建GSMUnitsPage的按钮证明一切正常。

登录视图模型:

class LoginViewModel
{
    public string Email { get; set; }
    public string Password { get; set; }
    public ICommand LoginCommand
    {
        get
        {
            return new Command(async () =>
            {
                ApiServices apiServices = new ApiServices();
                var accesstoken = await apiServices.LoginUserAsync(Email, Password);
                Settings.AccessToken = accesstoken;
            });
        }

    }

    public LoginViewModel()
    {
        Email = Settings.Username;
        Password = Settings.Password;
    }

}

登录:

public async Task<string> LoginUserAsync(string email, string password)
{
    var client = new HttpClient();

    var keyValues = new List<KeyValuePair<string, string>>
    {
        new KeyValuePair<string, string>("username", email),
        new KeyValuePair<string, string>("password", password),
        new KeyValuePair<string, string>("grant_type", "password")
    };

    var request = new HttpRequestMessage(
        HttpMethod.Post, "linktotoken");

    request.Content = new FormUrlEncodedContent(keyValues);
    var reponse = await client.SendAsync(request);

    var jwt = await reponse.Content.ReadAsStringAsync();
    JObject jwtDyanmic = JsonConvert.DeserializeObject<dynamic>(jwt);

    var accessToken = jwtDyanmic.Value<string>("access_token");
    var accessTokenExpiration = jwtDyanmic.Value<DateTime>(".expires");
    Settings.AccessTokenExpiration = accessTokenExpiration;

    Debug.WriteLine(jwt);

    return accessToken;
}

LoginPage.xaml:

<ContentPage.BindingContext>
<vm:LoginViewModel/>
</ContentPage.BindingContext>


<StackLayout VerticalOptions="Center">
<Entry Text="{Binding Email}" />
<Entry Text="{Binding Password}" />
<Button Command="{Binding LoginCommand}" Text="Login/Signin" />
</StackLayout>

App.xaml.cs

public App ()
{
    InitializeComponent();

    SetMainPage();
}

private void SetMainPage()
{
    if (!string.IsNullOrEmpty(Settings.AccessToken))
    {
        if (DateTime.UtcNow.AddHours(1) > Settings.AccessTokenExpiration)
        {
            var vm = new LoginViewModel();
            vm.LoginCommand.Execute(null);
        }
        MainPage = new NavigationPage(new GSMUnitsPage());
    }
    else if(!string.IsNullOrEmpty(Settings.Username) && !string.IsNullOrEmpty(Settings.Password))
    {
        MainPage = new NavigationPage(new LoginPage());
    }
    else
    {
        MainPage = new NavigationPage(new RegisterPage());
    }
}
提问于
用户回答回答于

在LoginCommand上设置Settings.AccessToken之后,尝试调用Appes.etMainPage()方法。(您可能想让SetMainPage公开)

 public ICommand LoginCommand
    {
        get
        {
            return new Command(async () =>
            {
                ApiServices apiServices = new ApiServices();
                var accesstoken = await apiServices.LoginUserAsync(Email, Password);
                Settings.AccessToken = accesstoken;
                App.SetMainPage();
            });
        }

    }

扫码关注云+社区

领取腾讯云代金券