3

我已经成功地让Akavache为 Windows 桌面、.NET 4.5 WPF 项目工作,但是当我尝试为 Xamarin(iOS 和 Android)目标构建它时,BlobCache单例没有正确初始化。BlobCache.Secure一片空白。(我已经尝试过 SQLite 和 'vanilla' 构建)

老实说,我发现 Akavache 的示例/文档有点薄。我不是 Reactive 的用户,我发现 Paul 的大部分代码都非常不透明。

我只是想为跨平台应用程序做一些非常简单、安全的应用程序状态缓存。

// 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());
    }
}
4

1 回答 1

8

所以,你现在必须在 Xamarin 上做一些愚蠢的技巧,我最近才发现这些技巧。我要将这些添加到文档中(或者在 Android 案例中,只需修复错误)

Xamarin.iOS

在 iOS 上,Type.GetType()不会加载程序集,这与任何其他平台都不相同。所以,你必须在你的 AppDelegate 中运行这个愚蠢的 goose 代码:

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

通常,此代码运行 AutoMagically™。

Xamarin.Android

注册在 Xamarin.Android 上运行良好,但由于我怀疑是 Akavache 中的错误,您可能必须注册 AutoSuspend(即使您不使用它)。

  1. 在你所有的活动中,声明AutoSuspendActivityHelper autoSuspendHelper;
  2. 在构造函数中,添加:

    autoSuspendHelper = new AutoSuspendActivityHelper(this);
    autoSuspendHelper.OnCreate(bundle);
    
  3. 覆盖OnPauseOnResumeOnSaveInstanceState并调用适当的autoSuspendHelper方法,即:

    autoSuspendHelper.OnPause();
    

更麻烦?

请通过 paul@github.com 给我发电子邮件或在github/akavache提交问题让我知道。我已经使用 Akavache 发布了一个在 iOS 和 Android 上运行的生产应用程序,它确实可以工作,但我意识到让这些东西正常工作可能有点 Tricky™。

于 2013-09-17T04:52:25.437 回答