【发布时间】:2017-11-13 01:26:28
【问题描述】:
当我在我的 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();
}
11-13 02:08:14.761 E/MobileCenterCrashes(6308):未处理的异常: 11-13 02:08:14.761 E/MobileCenterCrashes(6308): System.NullReferenceException:对象引用未设置为实例 的一个对象。 11-13 02:08:14.761 E/MobileCenterCrashes(6308):在 Xamarin.Forms.Platform.Android.AppCompat.Platform.LayoutRootPage (Xamarin.Forms.Page 页面,System.Int32 宽度,System.Int32 高度) [0x0000c] 在 D:\agent_work\1\s\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:291 11-13 02:08:14.761 E/MobileCenterCrashes(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_work\1\s\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:199 11-13 02:08:14.761 E/MobileCenterCrashes(6308):在 Xamarin.Forms.Platform.Android.PlatformRenderer.OnLayout (System.Boolean 改变,System.Int32 l,System.Int32 t,System.Int32 r, System.Int32 b) [0x0000e] 在 D:\agent_work\1\s\Xamarin.Forms.Platform.Android\PlatformRenderer.cs:73 11-13 02:08:14.761 E/MobileCenterCrashes(6308):在 Android.Views.ViewGroup.n_OnLayout_ZIIII (System.IntPtr jnienv, System.IntPtr native__this,System.Boolean 改变,System.Int32 l, System.Int32 t, System.Int32 r, System.Int32 b) [0x00008] in :0
编辑: 这个问题肯定是由 akavache 引起的。消息真的很奇怪。看起来 akavache 与 LayoutRootPage 有一些关系。
请参阅上面的代码,我从 akavache 缓存中获取用户对象,并且用户对象定义了我应该显示登录页面还是主页。如果我将 ShowMainPageOrLoginPage();function 移动到 akavache 调用之上,它就可以正常工作。因此,您似乎无法在 rootlayoutpage 之前使用 akavache 进行任何查询 - 主页已设置或加载。
【问题讨论】:
-
嗯不确定手头...我有一个使用移动中心和最新的 akavache github.com/PureWeen/Akavache.Samples 放在一起的示例,它似乎加载正常,没有任何异常
-
哪一行导致崩溃?堆栈跟踪看起来像是发生在布局中,而不是 OnStart
-
@ShaneNeuville 可能是因为 sample 正在使用下面建议的 jack_tux 这条线?为什么我需要? _LazyBlob = new Lazy
(() => new SQLitePersistentBlobCache(Path.Combine(fs.GetDefaultLocalMachineCacheDirectory(), "afile.db"), BlobCache.TaskpoolScheduler)); -
@Jason 消息真的很奇怪。我也不明白为什么它向我显示布局但它在 var User= await BlobCache.UserAccount.GetObject
("User");如果我删除这条线或者如果我在其他地方(如在任何视图模型中)执行这条线,一切都会正常工作。仅当我在 app.xaml.cs 的 OnStart 中执行此操作时才会崩溃 -
@ShaneNeuville 我只是仔细看了看,示例正在使用 MainPage.Appearing 从 akavache 获取对象,我说它工作正常。它不会直接在 onstart 方法中查询
标签: xamarin.forms xamarin.android akavache mobile-center visual-studio-app-center