简体   繁体   中英

Windows Azure Mobile Services

I'm creating a Windows Phone 8 App (c#) using Windows Azure Mobile Services to store my data. The sample I downloaded from azure as below:

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. I want to move the class FVItemData to InsertClass.cs. And I was try the following:

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!
}

How to call the InsertTodoItem function in InsertClass.cs from MainPage.xaml.cs? Please help, I was try whole day with nothing. I am new in 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' "

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. 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 :

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 .:

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. 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. What you likely need is a simple List<FVPerson> which can hold the items as you insert them.

You should change your MainPage.xaml.cs to use FV_Person class instead of TodoItem class

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

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? 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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