简体   繁体   English

如何使用Xamarin.Forms的Azure移动服务?

[英]How to use Azure Mobile Services from Xamarin.Forms?

I try to use the Azure Mobile Services from my simple Forms app and it don't work. 我尝试使用我的简单Forms应用程序中的Azure移动服务,它不起作用。 The last command just run forever. 最后一个命令只是永远运行。 I checked the internet connection with a WebClient and it is okay. 我用WebClient检查了互联网连接,没关系。 The code works fine in a simple Windows console application. 该代码在简单的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;

Edit : I use Xamarin Studio because I only have indie license. 编辑 :我使用Xamarin Studio因为我只有独立许可证。 For the debug I tried a Samsung Galaxy S3 and a Huawei Ascend P7, the result was same. 为了调试我尝试了三星Galaxy S3和华为Ascend P7,结果是一样的。 I have updated the Xamarin and now it produces an interesting exception if I use async-await to get the result. 我已经更新了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

Are you sure you are not blocking your UI with a combination of async / await (deadlock)? 你确定你没有使用async / await(死锁)的组合来阻止你的UI吗? Modify your code as follows: 修改您的代码如下:

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

See here for more Information about this specific problem: http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html 有关此特定问题的详细信息,请参阅此处: http//blog.stephencleary.com/2012/07/dont-block-on-async-code.html

For further informations about a deadlock, Stephen Clary has an excellent blog about this: http://blog.stephencleary.com/2012/02/async-and-await.html 有关死锁的更多信息,Stephen Clary有一个很好的博客: http//blog.stephencleary.com/2012/02/async-and-await.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM