c# – 如何从Xamarin.Forms中使用Azure Mobile Services?

前端之家收集整理的这篇文章主要介绍了c# – 如何从Xamarin.Forms中使用Azure Mobile Services?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试从我简单的Forms应用程序使用Azure Mobile Services,它不起作用.最后一个命令永远运行.我检查了一个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) <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

解决方法

你确定你没有阻止你的UI与组合的async / await(死锁)?
修改你的代码如下:
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

猜你在找的C#相关文章