簡體   English   中英

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

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

我已經成功地讓Akavache為Windows桌面,.NET 4.5 WPF項目工作,但是當我嘗試為Xamarin(iOS和Android)目標構建它時, BlobCache單例未正確初始化。 BlobCache.Secure為null。 (我已經嘗試了SQLite和'vanilla'構建)

老實說,我發現Akavache的示例/文檔有點薄。 我不是Reactive的用戶,我發現很多Paul的代碼非常不透明。

我只是想為跨平台應用程序做一些非常簡單,安全的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());
    }
}

所以,你現在必須在Xamarin上做一些愚蠢的伎倆,我最近才知道。 我要將這些添加到文檔中(或者在Android案例中,只修復bug)

Xamarin.iOS

在iOS上, Type.GetType()不會加載程序集,這與任何其他平台不同。 所以,你必須在你的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);

通常,此代碼運行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提交問題告訴我。 我已經發布了一個可以在iOS和Android上運行的Akavache的生產應用程序,它確實有效,但我意識到可能有點Tricky™可以讓它工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM