0

我尝试通过 Akavache 加载缓存的数据,但我不知道为什么我无法正确处理。我尝试获取登录后已经缓存的全名和电子邮件,所以我在我的“CachedUser”模型中获取对象,但不知道为什么它说那里没有全名和电子邮件的定义。这是我的 CachedUser 模型

namespace KGVC.Models
{
    public class CachedUsers
    {
        public string FullName { get; set; }
        public string Email { get; set; }
    }
}

这是我尝试实现的在 Akavache 中获取对象的代码

   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?)

这里有什么问题,因为我认为我的代码没有任何问题。你能弄清楚吗?

4

1 回答 1

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对象的概念,以更好地理解这些概念以及在这种特定情况下的问题所在。

于 2017-11-15T08:06:41.023 回答