与C#中的属性异步

时间:2016-08-17 12:51:11

标签: c# mvvm getter

我有一个房产。在getter中,我想调用异步方法。请查看我也标记了注释的代码: -

 public string AuthToken
{
    get
    {
        TimeSpan timediff = App.Locator.Login.TokenExpire_On.ToLocalTime() - DateTime.Now.ToLocalTime();
        if (timediff.TotalMinutes < 55)
            App.Locator.Login.GetRefreshToken();
        // before return the value i want to complete the work of GetRefreshToken(), GetRefreshToken is a async method.
        return AppSettings.GetValueOrDefault<string>(AuthTokenKey, AuthTokenDefault);
    }
    set
    {
        AppSettings.AddOrUpdateValue<string>(AuthTokenKey, value);
    }
}

public async Task GetRefreshToken()
{
    if (string.IsNullOrEmpty(this.AuthToken))
        return;
    AzureRefreshToken refreshToken = await UserProfileService.RefreshToken(this.AuthToken);
    Settings.AuthToken = refreshToken.RefreshToken;
    TokenExpire_On = DateTime.Now.AddHours(1);

}

1 个答案:

答案 0 :(得分:1)

也许

 if (timediff.TotalMinutes < 55)
        App.Locator.Login.GetRefreshToken().RunSynchronously();

可以解决问题吗?