简体   繁体   English

在Xamarin.Android/Xamarin.iOS中使用Akavache

[英]Using Akavache in Xamarin.Android/Xamarin.iOS

I've successfully got Akavache working for a Windows desktop, .NET 4.5 WPF project, but when I try to build it for the Xamarin (iOS and Android) targets, the BlobCache singleton is not properly initialized. 我已经成功地让Akavache为Windows桌面,.NET 4.5 WPF项目工作,但是当我尝试为Xamarin(iOS和Android)目标构建它时, BlobCache单例未正确初始化。 BlobCache.Secure is null. BlobCache.Secure为null。 (I've tried both the SQLite and 'vanilla' builds) (我已经尝试了SQLite和'vanilla'构建)

I'll be honest, I find the examples/documentation for Akavache a bit thin. 老实说,我发现Akavache的示例/文档有点薄。 I'm not a user of the Reactive stuff, I find much of Paul's code very opaque. 我不是Reactive的用户,我发现很多Paul的代码非常不透明。

I'm just trying to do some very simple, secure caching of app state for a cross-platform app. 我只是想为跨平台应用程序做一些非常简单,安全的app状态缓存。

// where we store the user's application state
BlobCache.ApplicationName = "myApp";
BlobCache.EnsureInitialized();

public AppState State
{
    get
    {
        return _appState;
    }
    set
    {
        _appState = value;
    }
}

public void Load()
{
    try
    {
        State = BlobCache.Secure.GetObjectAsync<AppState>.FirstOrDefault();
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.ToString());
        State = new AppState();
    }           
}

public void Save()
{
    try
    {           
        BlobCache.Secure.InsertObject("AppState", State);
    }
    catch(Exception ex)
    {
        Debug.WriteLine(ex.ToString());
    }
}

So, there are some dumb tricks you have to do right now on Xamarin, that I've only very recently found out. 所以,你现在必须在Xamarin上做一些愚蠢的伎俩,我最近才知道。 I'm going to add these to the docs (or in the Android case, just fix the bug) 我要将这些添加到文档中(或者在Android案例中,只修复bug)

Xamarin.iOS Xamarin.iOS

On iOS, Type.GetType() won't load assemblies, which isn't the same as any other platform. 在iOS上, Type.GetType()不会加载程序集,这与任何其他平台不同。 So, you have to run this silly goose code in your AppDelegate: 所以,你必须在你的AppDelegate中运行这个愚蠢的鹅代码:

var r = new ModernDependencyResolver();
(new ReactiveUI.Registrations()).Register((f,t) => r.Register(f, t));
(new ReactiveUI.Cocoa.Registrations()).Register((f,t) => r.Register(f, t));
(new ReactiveUI.Mobile.Registrations()).Register((f,t) => r.Register(f, t));

RxApp.DependencyResolver = r;
(new Akavache.Registrations()).Register(r.Register);
(new Akavache.Mobile.Registrations()).Register(r.Register);
(new Akavache.Sqlite3.Registrations()).Register(r.Register);

Normally, this code runs AutoMagically™. 通常,此代码运行AutoMagically™。

Xamarin.Android Xamarin.Android

Registration works fine on Xamarin.Android, but because of what I suspect is a bug in Akavache, you may have to register for AutoSuspend (even if you don't use it). 注册在Xamarin.Android上运行正常,但由于我怀疑是Akavache中的错误,您可能需要注册AutoSuspend(即使您不使用它)。

  1. In all your activities, declare AutoSuspendActivityHelper autoSuspendHelper; 在您的所有活动中,声明AutoSuspendActivityHelper autoSuspendHelper;
  2. In the constructor, add: 在构造函数中,添加:

     autoSuspendHelper = new AutoSuspendActivityHelper(this); autoSuspendHelper.OnCreate(bundle); 
  3. Override OnPause , OnResume , and OnSaveInstanceState and call the appropriate autoSuspendHelper method ie: 覆盖OnPauseOnResumeOnSaveInstanceState并调用相应的autoSuspendHelper方法,即:

     autoSuspendHelper.OnPause(); 

More trouble? 更麻烦?

Please let me know, either by Emailing me at paul@github.com or filing issues at github/akavache . 请通过发送电子邮件至paul@github.com或在github / akavache提交问题告诉我。 I've shipped a production application with Akavache that runs on both iOS and Android, and it definitely works, but I realize it might be a bit Tricky™ to get stuff to work. 我已经发布了一个可以在iOS和Android上运行的Akavache的生产应用程序,它确实有效,但我意识到可能有点Tricky™可以让它工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM