【问题标题】:Get information from Azure Mobile Service to Windows 8 app从 Azure 移动服务获取信息到 Windows 8 应用
【发布时间】:2014-03-19 00:53:55
【问题描述】:

我正在使用 Azure 移动服务在 C# 中开发一个 Windows 8 应用程序。这是我第一次使用 Azure,我已经弄清楚如何将数据从我的应用程序放入表中。我不能做的是根据表中的一位数据检索单个数据。例如,根据 ID 从移动服务中检索名称。

【问题讨论】:

    标签: c# azure windows-8 windows-8.1 azure-mobile-services


    【解决方案1】:

    如果要根据id来检索物品,可以使用LookupAsync方法:

    var table = MobileService.GetTable<MyDataType>();
    var item = await table.LookupAsync(id);
    

    如果你想基于另一个属性检索一个项目,你可以使用@Sopuli 提到的Where 子句,但是如果你想要的只是项目本身,你不需要为此创建一个集合:

    var table = MobileService.GetTable<MyDataType>();
    var items = await table.Where(d => d.Property == propValue).ToEnumerableAsync();
    var item = items.FirstOrDefault();
    

    【讨论】:

    • 非常感谢!工作很愉快,很简单。
    • 你能告诉我如何在给定 id 的点更新信息吗?会非常棒@carlosfigueira
    • 最简单的方法是查询项目(如下所示),更改对象中所需的任何属性,然后使用该对象调用UpdateAsync
    • 再次感谢您
    【解决方案2】:

    从 id=10000 的表中检索行;

    int WantedID = 10000;
    public IMobileServiceTable<SomeTable> MyTable = App.MobileService.GetTable<SomeTable>();
    MobileServiceCollection<SomeTable, SomeTable> MyList = new MobileServiceCollection<SomeTable, SomeTable>(MyTable.Where(t => t.id == WantedID));
    

    你的班级 SomeTable 会是这样的:

    using Newtonsoft.Json;
    ...
    public class SomeTable: INotifyPropertyChanged
    {
        private Int64 _id;
        [JsonProperty(PropertyName = "id")]
        public Int64 id
        {
            get
            {
                return _id;
            }
            set
            {
                if (value != _id)
                {
                    _id = value;
                    NotifyPropertyChanged("id");
                }
            }
        }
    
        private string _name;
        [JsonProperty(PropertyName = "name")]
        public string name
        {
            get
            {
                return _name;
            }
            set
            {
                if (value != _name)
                {
                    _name= value;
                    NotifyPropertyChanged("_name");
                }
            }
        }
    

    现在您的 MyList 已填充从 Azure 下载的数据,例如,您可以将其绑定到 LonglistSelector。如果您需要访问一个特定数据项的属性,您可以执行 MyList[0].name

    此外,我建议您的类 SomeTable 是您在 Azure 中的表的精确克隆,并且每个属性名称与表中的列名完全相同。

    【讨论】:

    • 感谢您的建议