简体   繁体   English

Windows Azure移动服务

[英]Windows Azure Mobile Services

I'm creating a Windows Phone 8 App (c#) using Windows Azure Mobile Services to store my data. 我正在使用Windows Azure移动服务创建一个Windows Phone 8应用程序(c#)来存储我的数据。 The sample I downloaded from azure as below: 我从azure下载的样本如下:

public class TodoItem
{
    public string Id { get; set; }

    [JsonProperty(PropertyName = "text")]
    public string Text { get; set; }

    [JsonProperty(PropertyName = "complete")]
    public bool Complete { get; set; }
}


public class FVItemData
{
    private MobileServiceCollection<Entity.FV_Person, Entity.FV_Person> items;
    private IMobileServiceTable<Entity.FV_Person> FVTable = App.MobileService.GetTable<Entity.FV_Person>();

    public async void InsertTodoItem(Entity.FV_Person fvItem)
    {
        await FVTable.InsertAsync(fvItem);
        items.Add(fvItem);
    }
}

But now I already create a new cs file which name InsertClass.cs. 但是现在我已经创建了一个名为InsertClass.cs的新cs文件。 I want to move the class FVItemData to InsertClass.cs. 我想将类FVItemData移动到InsertClass.cs。 And I was try the following: 我尝试以下方法:

InsertClass.cs InsertClass.cs

namespace GetStartedWithData.ViewModel
{
    public class FVItemData
    {
        private MobileServiceCollection<FV_Person, FV_Person> items;
        private IMobileServiceTable<FV_Person> FVTable = App.MobileService.GetTable<FV_Person>();

        private async void InsertTodoItem(FV_Person fvItem)
        {
            await FVTable.InsertAsync(fvItem);
            items.Add(fvItem);
        }
    }
}

MainPage.xaml.cs MainPage.xaml.cs中

private void ButtonSave_Click(object sender, RoutedEventArgs e)
{
     var FVItem = new FV_Person { Text = InputText.Text };
     FVItemData.InsertPerson(FVItem); <--- I try this but error!
}

How to call the InsertTodoItem function in InsertClass.cs from MainPage.xaml.cs? 如何从MainPage.xaml.cs中调用InsertClass.cs中的InsertTodoItem函数? Please help, I was try whole day with nothing. 请帮忙,我一整天都没有尝试。 I am new in C#. 我是C#的新手。 Thanks... 谢谢...

Updated: 更新:

I had modified the question, but I have the error have same line, the error message is " 'GetStartedWithData.ViewModel.FVItemData' does not contain definition of 'InsertPerson' " 我修改了这个问题,但是我的错误有相同的行,错误信息是“'GetStartedWithData.ViewModel.FVItemData'不包含'InsertPerson'的定义”

There are a few problems in your code: 您的代码中存在一些问题:

Build issues 建立问题

You're trying to access an instance method ( FVItemData.InsertTodoItem ) by using the class name directly. 您正在尝试直接使用类名访问实例方法( FVItemData.InsertTodoItem )。 Instance methods need to be accessed via instances (ie, objects of that class). 需要通过实例(即该类的对象)访问实例方法。 You can either create an instance of FVItemData and then call InsertTodoItem : 您可以创建FVItemData的实例,然后调用InsertTodoItem

private void ButtonSave_Click(object sender, RoutedEventArgs e)
{
    var FVItem = new FV_Person { Text = InputText.Text };
    var fvItemData = new FVItemData();
    fvItemData.InsertPerson(FVItem);
}

Or you can make it that a static method, which you can access via the class itself - notice that you may also need to make the FVTable field static .: 或者你可以使它成为一个静态方法,你可以通过类本身访问 - 注意你可能还需要使FVTable字段static

public class FVItemData
{
    //...

    private async void InsertTodoItem(FV_Person fvItem)
    {
        await FVTable.InsertAsync(fvItem);
        items.Add(fvItem);
    }
}

Runtime issues 运行时问题

Once the build issue is fixed, you'll likely have a NullReferenceException in the call to items.Add(fvItem) - since items wasn't initialized, it will be null. 修复构建问题后,在对items.Add(fvItem)的调用中可能会出现NullReferenceException - 因为项目未初始化,所以它将为null。 You're using a MobileServiceCollection<T1,T2> as the type for the items field, but that type should only be used when you're using data binding with some UI control - and you also shouldn't insert items to the collection directly. 您正在使用MobileServiceCollection<T1,T2>作为items字段的类型,但只有当您使用带有某些UI控件的数据绑定时才应使用该类型 - 并且您也不应该直接将项目插入到集合中。 What you likely need is a simple List<FVPerson> which can hold the items as you insert them. 您可能需要的是一个简单的List<FVPerson> ,它可以在您插入项目时保存这些项目。

You should change your MainPage.xaml.cs to use FV_Person class instead of TodoItem class 您应该更改MainPage.xaml.cs以使用FV_Person类而不是TodoItem类

private void ButtonSave_Click(object sender, RoutedEventArgs e)
{
    var FVItem= new FV_Person { attributes };
    FVItemData.InsertPerson(FVItem);
}

Also to make stuff clearer you should also change the method on InsertClass.cs 另外,为了使内容更清晰,您还应该更改InsertClass.cs上的方法

public class FVItemData
{
    private MobileServiceCollection<FV_Person, FV_Person> items;
    private IMobileServiceTable<FV_Person> FVTable = App.MobileService.GetTable<FV_Person>();

    private async void InsertPerson(FV_Person fvItem)
    {
        await FVTable.InsertAsync(fvItem);
        items.Add(fvItem);
    }
}

One obvious question, have you created the table FIRST in WAMS? 一个显而易见的问题,你是否在WAMS中创建了表格FIRST? You don't have to add columns, that is done when you insert the first record. 您不必添加列,这是在插入第一条记录时完成的。

So check table is created before you insert the data (tables are not created automatically) 因此在插入数据之前会创建检查表(不会自动创建表)

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

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