【发布时间】:2016-07-19 06:58:30
【问题描述】:
我需要在我的应用程序中使用 Azure 移动服务,并在我的项目中添加 Azure 移动客户端 SDK。为了在我的项目中实现 Web 服务,我像这样设置了我的 AzureService 类-
public partial class AzureService
{
static AzureService defaultInstance = new AzureService ();
MobileServiceClient client;
IMobileServiceTable<modelLifeStyleDiets> modelLifeStyleDietsTable;
public AzureService ()
{
this.client = new MobileServiceClient(url);
this.modelLifeStyleDietsTable = client.GetTable<modelLifeStyleDiets>();
}
public static AzureService DefaultManager{
get{
return defaultInstance;
}
private set{
defaultInstance = value;
}
}
public MobileServiceClient CurrentClient{
get{ return client;}
}
public bool IsOfflineEnabled{
get{ return modelLifeStyleDietsTable is Microsoft.WindowsAzure.MobileServices.Sync.IMobileServiceSyncTable<modelLifeStyleDiets>; }
}
public async Task<ObservableCollection<modelLifeStyleDiets>> GetTodoItemsAsync (bool syncItems = false)
{
try {
IEnumerable<modelLifeStyleDiets> items = await modelLifeStyleDietsTable
.Where(Item => !(Item.done)).ToEnumerableAsync ();
return new ObservableCollection<modelLifeStyleDiets> (items);
} catch (MobileServiceInvalidOperationException msioe) {
Debug.WriteLine (@"Invalid sync operation: {0}", msioe.Message);
} catch (Exception e) {
Debug.WriteLine (@"Sync error: {0}", e.Message);
}
return null;
}
public async Task SaveTaskAsync (modelLifeStyleDiets item)
{
if (item.id == null) {
await modelLifeStyleDietsTable.InsertAsync (item);
} else {
await modelLifeStyleDietsTable.UpdateAsync (item);
}
}
}
我的服务模型如下-
public class modelLifeStyleDiets
{
public string id { get; set; }
public string itemName { get; set; }
public bool done{ get; set;}
}
我有 ListView,我需要从服务中获取数据并将其分配给 ListView 的 ItemSource。 我试过这个 - listof_lifestyleDiets.ItemsSource=AzureService.DefaultManager.GetTodoItemsAsync();
但在上面的代码中,我收到了缺少类型转换的错误。如何解决?
【问题讨论】: