1

我正在尝试在 Xamarin Forms PCL 项目中使用 Azure 移动服务,但遇到了无法解决的错误。

我在这个链接上关注了 Azure 的官方文档。

****理由****

  1. 我已经从 NuGet 包安装了 Windows Azure 移动服务,并将CurrentPlatform.Init();&using Microsoft.WindowsAzure.MobileServices;放入每个平台。
  2. 这是我发现在 Xamarin Forms 中实现 Azure 移动服务的一些教程。我已经尝试过了,但仍然无法正常工作。这是链接---> 链接1,链接2,链接3

这是我的项目代码:

模型

using System;
using Newtonsoft.Json;

namespace eRestaurant.Model
{
    public class Restaurant
    {
        public string Id { get; set; }

        [JsonProperty(PropertyName = "restaurantname")]
        public string RestaurantName { get; set; }

        [JsonProperty(PropertyName = "tag")]
        public string Tag { get; set; }

        public Restaurant()
        {

        }
    }
}

using eRestaurant.Model;
using eRestaurant.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using Xamarin.Forms;

namespace eRestaurant
{
    public class RestaurantPage : ContentPage
    {
        RestaurantViewModel viewModel = new RestaurantViewModel();
        ListView RestaurantListView = new ListView();

        public RestaurantPage()
        {
            Title = "Restaurants";
            Style = AppStyle.PageStyle;
            RestaurantListView.RowHeight = 40;

            RestaurantListView.ItemsSource = viewModel.restaurantData;
            RestaurantListView.ItemTemplate = new DataTemplate(typeof(RestaurantViewCell));
            Content = RestaurantListView;
        }
    }
}

视图模型

using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.WindowsAzure.MobileServices; //Azure Mobile Service
using System.Threading.Tasks; //For multithreading
using eRestaurant.Model;

namespace eRestaurant.ViewModel
{
    public class RestaurantViewModel
    {
        private IMobileServiceTable<Restaurant> restaurantTable;

        const string applicationURL = @"https://erestaurant.azure-mobile.net/";
        const string applicationKey = @"MyApplicationKey";

        MobileServiceClient client = new MobileServiceClient( applicationURL, applicationKey);


        public IEnumerable<Restaurant> restaurantData;

        public RestaurantViewModel()
        {
            GetData ();
        }

        public async void GetData(){
            restaurantTable = client.GetTable<Restaurant> ();
            restaurantData = await restaurantTable
            .Where (restaurant => restaurant.RestaurantName != null)
            .ToListAsync ();
        }

    }
}

ViewCell 作为 ItemTemplate

using System;
using Xamarin.Forms;
using System.Collections.Generic;
using eRestaurant.Model;

namespace eRestaurant
{
    public class RestaurantViewCell : ViewCell
    {
        public RestaurantViewCell()
        {    
            Label restaurantName = new Label();
            restaurantName.Style = AppStyle.RestaurantLabelStyle;

            Label tag = new Label();
            tag.Style = AppStyle.TagLabelStyle;

            restaurantName.SetBinding(Label.TextProperty, "RestaurantName");
            tag.SetBinding(Label.TextProperty, "Tag");

            StackLayout nameLayout = new StackLayout()
            {
                HorizontalOptions = LayoutOptions.StartAndExpand,
                Orientation = StackOrientation.Vertical,
                Children = { restaurantName, tag }
            };

            View = nameLayout;
        }
    }
}

特此显示的错误消息。

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:698)
  at Caused by: java.lang.reflect.InvocationTargetException
  at at java.lang.reflect.Method.invoke(Native Method)
  at at java.lang.reflect.Method.invoke(Method.java:372)
  at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
  at ... 1 more
  at Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException: Error: Table 'Restaurant' does not exist.
  at at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x0004b>
  at at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (object) <IL 0x00006, 0x0003b>
  at Android.App.SyncContext/<Post>c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/src/Android.App/SyncContext.cs:18
  at Java.Lang.Thread/RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/src/Java.Lang/Thread.cs:36
  at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) [0x00009] in /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Java.Lang.IRunnable.cs:71
  at at (wrapper dynamic-method) object.061cf032-901c-4d7a-8b86-094723bf4132 (intptr,intptr) <IL 0x00011, 0x00027>
  at at mono.java.lang.RunnableImplementor.n_run(Native Method)
  at at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29)
  at at android.os.Handler.handleCallback(Handler.java:739)
  at at android.os.Handler.dispatchMessage(Handler.java:95)
  at at android.os.Looper.loop(Looper.java:135)
  at at android.app.ActivityThread.main(ActivityThread.java:5254)
  at ... 4 more
4

0 回答 0