【问题标题】:How to use Azure Mobile Services from Xamarin.Forms?如何从 Xamarin.Forms 使用 Azure 移动服务?
【发布时间】:2015-07-06 11:15:56
【问题描述】:

我尝试从我的简单表单应用程序中使用 Azure 移动服务,但它不起作用。最后一个命令永远运行。我用 WebClient 检查了互联网连接,没问题。该代码在简单的 Windows 控制台应用程序中运行良好。

    var ms = new MobileServiceClient(
            @"https://xxx.azure-mobile.net/",
            @"xxx"
        );

    IMobileServiceTable<TodoItem> todoTable =
        ms.GetTable<TodoItem>();
    List<TodoItem> items = todoTable
        .Where(todoItem => todoItem.Complete == false).ToListAsync().Result;

编辑: 我使用 Xamarin Studio 因为我只有独立许可证。对于调试,我尝试了三星 Galaxy S3 和华为 Ascend P7,结果是一样的。我已经更新了 Xamarin,现在如果我使用 async-await 来获取结果,它会产生一个有趣的异常。

Java.Lang.RuntimeException: java.lang.reflect.InvocationTargetException
  at --- End of managed exception stack trace ---
  at java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
  at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:675)
  at at dalvik.system.NativeStart.main(Native Method)
  at Caused by: java.lang.reflect.InvocationTargetException
  at at java.lang.reflect.Method.invokeNative(Native Method)
  at at java.lang.reflect.Method.invoke(Method.java:515)
  at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:859)
  at ... 2 more
  at Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
  at at Xamarin.Forms.Platform.Android.FormsApplicationActivity.OnPrepareOptionsMenu (Android.Views.IMenu) <IL 0x00007, 0x00050>
  at Android.App.Activity.n_OnPrepareOptionsMenu_Landroid_view_Menu_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d23da369/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.App.Activity.cs:4151
  at at (wrapper dynamic-method) object.fcd55d8e-49be-4c8f-b3a6-37be6bdb988f (intptr,intptr,intptr) <IL 0x00017, 0x0005b>
  at at md5530bd51e982e6e7b340b73e88efe666e.FormsApplicationActivity.n_onPrepareOptionsMenu(Native Method)
  at at md5530bd51e982e6e7b340b73e88efe666e.FormsApplicationActivity.onPrepareOptionsMenu(FormsApplicationActivity.java:110)
  at at android.app.Activity.onPreparePanel(Activity.java:2612)
  at at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:518)
  at at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:872)
  at at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:243)
  at at android.view.Choreographer$CallbackRecord.run(Choreographer.java:780)
  at at android.view.Choreographer.doCallbacks(Choreographer.java:593)
  at at android.view.Choreographer.doFrame(Choreographer.java:561)
  at at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:766)
  at at android.os.Handler.handleCallback(Handler.java:733)
  at at android.os.Handler.dispatchMessage(Handler.java:95)
  at at android.os.Looper.loop(Looper.java:136)
  at at android.app.ActivityThread.main(ActivityThread.java:5290)
  at ... 5 more

【问题讨论】:

  • 你试过await吗?我在 TPL 和网络方面都看到了粗糙的边缘 - 特别是超时不起作用
  • 我试过了。如果我尝试调试控件,只需在调用后退出该函数,并且永远不会到达下一个命令。我尝试使用以下代码: List items = await todoTable .Where (todoItem => todoItem.Complete == false).ToListAsync ();
  • 您使用的是最新版本的 Xamarin - 仔细检查最新版本吗?你测试的是什么设备,只是一台PC?调试和运行时没有区别?如果是,您使用的是 Xamarin Studio 还是 VS 插件?
  • 我已经更新了我的问题。

标签: c# azure xamarin azure-mobile-services xamarin.forms


【解决方案1】:

您确定您没有使用 async / await(死锁)的组合来阻塞您的 UI 吗? 修改你的代码如下:

IMobileServiceTable<TodoItem> todoTable = await ms.GetTable<TodoItem>();
List<TodoItem> items = todoTable.Where(todoItem => todoItem.Complete == false).ToListAsync().ConfigureAwait(false);

有关此特定问题的更多信息,请参见此处:http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

有关死锁的更多信息,Stephen Clary 有一篇很棒的博客:http://blog.stephencleary.com/2012/02/async-and-await.html

【讨论】:

    猜你喜欢
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    相关资源
    最近更新 更多