【问题标题】:Manipulate Windows Azure Mobile Services table操作 Windows Azure 移动服务表
【发布时间】:2013-12-30 21:21:26
【问题描述】:

我正在尝试用 C# 构建一个操作 azure 移动服务表的 Windows 商店应用程序。我看了教程,但发现自己有一些错误:

1。我无法读取数据,我想我根本不懂教程。我可以修改 TodoItem 表,但是当涉及到不同的表(当然来自 azure 移动服务)时,我无法读取它:

我的 C# 代码:

using Microsoft.WindowsAzure.MobileServices;
using Newtonsoft.Json;

public class myTable
{
    [JsonProperty(PropertyName = "column1")]
    public string column1 { get; set; }

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

public MobileServiceCollection<myTable, myTable> collection;
public IMobileServiceTable<myTable> table = App.MobileService.GetTable<myTable>();
public static MobileServiceClient client = new MobileServiceClient(
        "https://myMobileService.azure-mobile.net",
        "xxxxxxxxxxxxxxxxxxxxxxxxxx"
);

//at this point I am pretty much lost... :s ...

天蓝色教程中的家伙使用以下方法 在 TodoItem 表中查询,但我还是不明白...

todoItem 是从哪里来的,我看不到它的任何 Initialize?现在,如何使用自己的代码执行查询????

 private async void RefreshTodoItems()
 {                       
  // This query filters out completed TodoItems. 
  items = await todoTable
   .Where(todoItem => todoItem.Complete == false)
   .ToCollectionAsync();

  ListItems.ItemsSource = items;
 }

我有点迷路了,谢谢你的帮助。

【问题讨论】:

  • 您在GetTable 调用中使用的fuel 类的定义是什么?
  • 对不起,我本来打算写 myTable...

标签: c# sql windows-store-apps azure-mobile-services


【解决方案1】:

您需要一些东西。首先,您用于表格的类型(在您的情况下为myTable)应该有一个“id”字段。

public class myTable
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("column1")]
    public string Column1 { get; set; }

    [JsonProperty("column2")]
    public string Column2 { get; set; }
}

现在,针对您的具体问题:

tod​​oItem 来自哪里,我看不到它的任何 Initialize?

传递给Where 方法的是一个lambda 表达式 (todoItem =&gt; todoItem.Complete == false)。您可以将其理解为“对于表中的所有元素,我将其称为‘todoItem’,给我那些该元素的 Complete 属性为 false 的元素”。在内部,这被转换为发送到服务器的查询,但这是一个实现细节。您可以为此使用任何“名称”。例如,如果要查询表中的数据,可以编写如下代码:

var result = await table
    .Where(x => x.Column1 == "value" && x.Column2.IndexOf("foo") < 0)
    .ToListAsync();

您最终会得到一个表中的项目列表(存储在服务中),其属性满足传递给Where 方法的表达式。

另外需要注意的是:从IMobileServiceTable&lt;T&gt; 接口的实例中读取数据有两种方法:使用ToListAsync(或ToEnumerableAsync)和ToCollectionAsync。前者为您提供可以直接枚举和使用的数据;后者为您提供了一个集合,您可以将其设置为控件的 ItemsSource 属性,例如 ListView。

【讨论】:

    【解决方案2】:

    //这个代码只在xmal.cs里面对大家很有用

    试试 { IEnumerable 电子邮件 = 等待 App.MobileService.GetTable() .Where(x => x.email == tbemail.Text && x.pwd == tbpswd.Password) .Select(x => x.email) .ToEnumerableAsync();

                m = email.FirstOrDefault();
                m1 = "login successful...";
                if (m != null)
                    this.Frame.Navigate(typeof(login2));
                else
                    m1 = "invalid user id or password...";
            }
            catch
            {
    
            }
    
    
            var dialog = new MessageDialog(m1);
            dialog.Commands.Add(new UICommand("OK"));
            await dialog.ShowAsync();
    

    }

    公共字符串 m1 { 获取;放; } 公共字符串 m { 获取;放; }

    【讨论】:

    • 感谢您分享您的代码,尽管我已经解决了我的 Azure“问题”:-)
    猜你喜欢
    • 2013-01-29
    • 1970-01-01
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多