我尝试在Xamarin Forms PCL Project中使用Azure Mobile Service但我遇到了这个错误,我无法解决它。
我已按照Azure link的官方文档进行操作。
**** ****理由
CurrentPlatform.Init();
&每个平台都using Microsoft.WindowsAzure.MobileServices;
。这是我项目的代码:
模型
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 As 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