Xamarin.forms 在调用OnStart方法时,Akavache或Sqlite正在崩溃

Xamarin.forms 在调用OnStart方法时,Akavache或Sqlite正在崩溃,xamarin.forms,xamarin.android,akavache,mobile-center,visual-studio-app-center,Xamarin.forms,Xamarin.android,Akavache,Mobile Center,Visual Studio App Center,当我在xamarin.forms项目中使用下面的代码检查用户对象是否存在于akavache缓存中时,我得到了下面的异常。相同的代码或任何akavache查询在其他地方工作,但仅在onStart方法中崩溃。我相信我已经在构造函数中初始化了akavache。我尝试使用完全相同的代码使用移动中心查询本地(本地sqlite)用户数据,但得到了相同的异常。我认为这应该和sqlite有关,因为akavache和mobile center都使用类似的sqlite库。有人知道为什么OnStart方法不起作用吗

当我在xamarin.forms项目中使用下面的代码检查用户对象是否存在于akavache缓存中时,我得到了下面的异常。相同的代码或任何akavache查询在其他地方工作,但仅在onStart方法中崩溃。我相信我已经在构造函数中初始化了akavache。我尝试使用完全相同的代码使用移动中心查询本地(本地sqlite)用户数据,但得到了相同的异常。我认为这应该和sqlite有关,因为akavache和mobile center都使用类似的sqlite库。有人知道为什么OnStart方法不起作用吗

 public App()
        {
            Microsoft.Azure.Mobile.MobileCenter.Start("android=key" +
                   "uwp=key",
                   typeof(Microsoft.Azure.Mobile.Analytics.Analytics), typeof(Microsoft.Azure.Mobile.Crashes.Crashes));

            Akavache.BlobCache.ApplicationName = "myApp";
            Akavache.BlobCache.EnsureInitialized();

            ServiceLocator.Add<ICloudService, AzureCloudService>();

            InitializeComponent();

            }

  protected async override void OnStart()
        { 

            try
            {
               var User= await BlobCache.UserAccount.GetObject<User>("User");

                if (User != null)
                    Helpers.Settings.IsLoggedIn = true;
                else
                    Helpers.Settings.IsLoggedIn = false;
            }
            catch (Exception)
            {
                Helpers.Settings.IsLoggedIn = false;
            }
           ShowMainPageOrLoginPage();
        }
public应用程序()
{
Microsoft.Azure.Mobile.MobileCenter.Start(“android=key”+
“uwp=key”,
typeof(Microsoft.Azure.Mobile.Analytics.Analytics),typeof(Microsoft.Azure.Mobile.Crash.Crash));
Akavache.BlobCache.ApplicationName=“myApp”;
Akavache.BlobCache.EnsureInitialized();
ServiceLocator.Add();
初始化组件();
}
受保护的异步重写void OnStart()
{ 
尝试
{
var User=await BlobCache.UserAccount.GetObject(“用户”);
如果(用户!=null)
Helpers.Settings.IsLoggedIn=true;
其他的
Helpers.Settings.IsLoggedIn=false;
}
捕获(例外)
{
Helpers.Settings.IsLoggedIn=false;
}
ShowMain页面或登录页面();
}
11-13 02:08:14.761 E/MobileCentercrasks(6308):未处理的异常: 11-13 02:08:14.761 E/MobileCenterprisks(6308): System.NullReferenceException:对象引用未设置为实例 指一个物体。11-13 02:08:14.761 E/MobileCenterprisks(6308):在 Xamarin.Forms.Platform.Android.AppCompat.Platform.LayoutRootPage (Xamarin.Forms.Page页面,System.Int32宽度,System.Int32高度) [0x0000c]英寸 D:\agent\u work\1\s\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:291 11-13 02:08:14.761 E/MobileCenterprisks(6308):在 Xamarin.Forms.Platform.Android.AppCompat.Platform.Xamarin.Forms.Platform.Android.IPlatformLayout.OnLayout (System.Boolean已更改,System.Int32 l,System.Int32 t,System.Int32 r、 System.Int32 b)[0x00003]英寸 D:\agent\u work\1\s\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:199 11-13 02:08:14.761 E/MobileCenterprisks(6308):在 Xamarin.Forms.Platform.Android.PlatformRenderer.OnLayout (System.Boolean已更改,System.Int32 l,System.Int32 t,System.Int32 r、 System.Int32 b)[0x0000e]in D:\agent\u work\1\s\Xamarin.Forms.Platform.Android\PlatformRenderer.cs:73 11-13 02:08:14.761 E/MobileCenterprisks(6308):在 Android.Views.ViewGroup.n_OnLayout_ZIIII(System.IntPtr jnienv, System.IntPtr native\uu此,System.Boolean已更改,System.Int32 l, System.Int32 t,System.Int32 r,System.Int32 b)[0x00008]输入 :0

编辑: 这个问题肯定是由阿卡瓦什引起的。这个消息真的很奇怪。看起来akavache和LayoutRootPage有些关系


查看上面的代码,我从akavache缓存中获取用户对象,用户对象定义我应该显示登录页面还是主页。如果我移动
ShowMainPageOrLoginPage()函数上面的akavache调用,它工作得很好。因此,在设置或加载rootlayoutpage-主页之前,您似乎无法使用akavache进行任何查询。

我以前也遇到过同样的问题,出于某种原因,如果您使用静态方法进行初始化,它不会一直工作

不起作用

IBlobCache _cache = BlobCache.LocalMachine;
行得通

IBlobCache _cache = new SQLitePersistentBlobCache(systemPath + "/CacheUtils.db");
如果你想找到systemPath,你可以在Android或iOS中使用它

systemPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);

嗯,现在还不确定。。。我收集了一个使用移动中心和最新的akavache的示例,它的加载似乎正常,没有任何例外哪条线路导致了崩溃?堆栈跟踪看起来像是在布局中发生的,而不是OnStart@ShaneNeuville这可能是因为样本使用的是jack_tux在下面建议的这条线吗?为什么我需要_LazyBlob=newlazy(()=>newsqlitepersistentblobcache(Path.Combine(fs.getdefaultlocalmachinecacheditory(),“afile.db”),BlobCache.TaskpoolScheduler))@杰森的留言真的很奇怪。我也不明白为什么它显示我的布局,但它在var User=await BlobCache.UserAccount.GetObject(“User”)行崩溃;如果我删除这一行,或者在其他地方执行这一行,就像在任何viewmodel中一样,一切都正常。只有在app.xaml的OnStart中执行此操作时才会崩溃。cs@ShaneNeuville我只是仔细看了一下,样本正在使用主页。似乎是从akavache那里得到的对象,我说工作正常。它不直接在onstart方法中查询这真的很奇怪。为什么它可以在任何其他页面上工作而不使用完整路径初始化,但在OnStart方法中不工作