当我在xamarin.forms项目中使用下面的代码检查用户对象是否存在于akavache缓存中时,我将得到下面的异常。相同的代码或任何akavache查询都在其他地方工作,但只在onStart方法中崩溃。我相信我已经在构造函数中初始化akavache了。我尝试使用移动中心( mobile )查询本地(本地sqlite)用户数据,并得到了相同的异常。我认为这应该与sqlite有关,因为akavache和移动中心都使用类似的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();
}11-13 02:08:14.761 E/MobileCenterCrashes( 6308):未处理异常:11-1302:08:14.761 E/MobileCenterCrashes( 6308):System.NullReferenceException:对象引用未设置为对象实例。11-13 02:08:14.761 E/MobileCenterCrashes( 6308):at Xamarin.Forms.Platform.Android.AppCompat.Platform.LayoutRootPage (Xamarin.Forms.Page页面,System.Int32宽度,D:\agent_work\1\s\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:291 11-13 02:08:14.761 E/MobileCenterCrashes( 6308):at Xamarin.Forms.Platform.Android.AppCompat.Platform.Xamarin.Forms.Platform.Android.IPlatformLayout.OnLayout (System.Boolean changed,System.Int32 l,System.Int32 t,System.Int32 r,D:\agent_work\1\s\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:199 11-13 02:08:14.761 E/MobileCenterCrashes( 6308):at Xamarin.Forms.Platform.Android.PlatformRenderer.OnLayout (System.Boolean changed,System.Int32 l,System.Int32 t,System.Int32 r,D:\agent_work\1\s\Xamarin.Forms.Platform.Android\PlatformRenderer.cs:73 11-13 02:08:14.761 E/MobileCenterCrashes( 6308):at Android.Views.ViewGroup.n_OnLayout_ZIIII (System.IntPtr jnienv,System.IntPtr native__this,System.Boolean changed,System.Int32 l,System.Int32 t,System.Int32 r,System.Int32 b) 0x00008 in :0
编辑:这个问题肯定是由akavache引起的。信息真的很奇怪。看起来,akavache和LayoutRootPage有某种关系。
参见上面的代码,我从akavache缓存中获得了用户对象,并且用户对象定义了我是应该显示登录页还是主页。如果我将ShowMainPageOrLoginPage();函数移动到akavache调用之上,它就能正常工作。因此,在设置或加载rootlayoutpage -主页之前,您似乎无法使用akavache进行任何查询。
发布于 2017-11-13 03:58:45
我以前也遇到过同样的问题,由于某种原因,如果您使用静态方法进行初始化,它不会一直工作。
不管用
IBlobCache _cache = BlobCache.LocalMachine;行得通
IBlobCache _cache = new SQLitePersistentBlobCache(systemPath + "/CacheUtils.db");如果你想找到systemPath,你可以在你的安卓或者iOS中使用它。
systemPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);https://stackoverflow.com/questions/47255888
复制相似问题