繁体   English   中英

Windows Azure移动服务

[英]Windows Azure Mobile Services

我正在使用Windows Azure移动服务创建一个Windows Phone 8应用程序(c#)来存储我的数据。 我从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);
    }
}

但是现在我已经创建了一个名为InsertClass.cs的新cs文件。 我想将类FVItemData移动到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中

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

如何从MainPage.xaml.cs中调用InsertClass.cs中的InsertTodoItem函数? 请帮忙,我一整天都没有尝试。 我是C#的新手。 谢谢...

更新:

我修改了这个问题,但是我的错误有相同的行,错误信息是“'GetStartedWithData.ViewModel.FVItemData'不包含'InsertPerson'的定义”

您的代码中存在一些问题:

建立问题

您正在尝试直接使用类名访问实例方法( FVItemData.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);
}

或者你可以使它成为一个静态方法,你可以通过类本身访问 - 注意你可能还需要使FVTable字段static

public class FVItemData
{
    //...

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

运行时问题

修复构建问题后,在对items.Add(fvItem)的调用中可能会出现NullReferenceException - 因为项目未初始化,所以它将为null。 您正在使用MobileServiceCollection<T1,T2>作为items字段的类型,但只有当您使用带有某些UI控件的数据绑定时才应使用该类型 - 并且您也不应该直接将项目插入到集合中。 您可能需要的是一个简单的List<FVPerson> ,它可以在您插入项目时保存这些项目。

您应该更改MainPage.xaml.cs以使用FV_Person类而不是TodoItem类

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

另外,为了使内容更清晰,您还应该更改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);
    }
}

一个显而易见的问题,你是否在WAMS中创建了表格FIRST? 您不必添加列,这是在插入第一条记录时完成的。

因此在插入数据之前会创建检查表(不会自动创建表)

暂无
暂无

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

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