我尝试通过Akavache加载缓存数据,但我不知道为什么我不能正确。我尝试获取登录后已经缓存的FullName和Email,所以我在我的" CachedUser"模型但不知道为什么它说没有FullName和Email的定义。 这是我的CachedUser模型
namespace KGVC.Models
{
public class CachedUsers
{
public string FullName { get; set; }
public string Email { get; set; }
}
}
这是我试图实现
的Akavache中的getobject代码 public void GetDataCache(object sender, EventArgs e)
{
var loaded = BlobCache.LocalMachine.GetObject<CachedUsers>("usercached");
txtemail.Text = loaded.FullName;
txtfullname.Text = loaded.FullName.ToString();
}
这是缓存用户代码
public void CacheUser(AuthenticationResult ar)
{
JObject user = ParseIdToken(ar.IdToken);
var cache = new CachedUsers
{
FullName = user["name"]?.ToString(),
Email = user["emails"]?.ToString()
};
BlobCache.LocalMachine.InsertObject("usercached", cache);
}
这是我得到的完整错误消息
'IObservable<CachedUsers>' does not contain a definition for 'FullName' and no extension method 'FullName' accepting a first argument of type 'IObservable<CachedUsers>' could be found (are you missing a using directive or an assembly reference?)
这里有什么问题,因为我认为我的代码没有任何问题。你能想出来吗?
答案 0 :(得分:1)
您需要等待它
public async void GetDataCache(object sender, EventArgs e)
{
var loaded = await BlobCache.LocalMachine.GetObject<CachedUsers>("usercached");
txtemail.Text = loaded.FullName;
txtfullname.Text = loaded.FullName.ToString();
}
或订阅IObservable
:
public async void GetDataCache(object sender, EventArgs e)
{
var loaded = await BlobCache.LocalMachine.GetObject<CachedUsers>("usercached").Subscribe(user => {
// TODO might need to wrap this in a Device.BeginInvokeOnMainThread
txtemail.Text = user.FullName;
txtfullname.Text = user.FullName.ToString();
});
}
查看async / await和IObservable
对象的概念,以便更好地理解这些特定情况下的概念和问题。