【发布时间】:2014-10-03 05:57:52
【问题描述】:
我正在使用 mvvm 概念(数据、组、模型)开发一个 win phone 8 应用程序,我使用这个概念完成了我的应用程序设计。现在我正在尝试将我的应用程序连接到 azure DB,我还通过以下代码使用 MVVM 概念连接了 azure DB,并且它工作成功。
var js = new JObject { { "institutionid", obj.institutionid }, { "userid", obj.userid } };
var result = await App.MobileService.InvokeApiAsync("school_365_create_dynamic_tile", js, System.Net.Http.HttpMethod.Post, null);
在创建 ViewModel 时,我必须调用使用 Azure 移动服务 Sdk 从 Azure 移动服务读取数据的服务。
Sdk api 使用 async /await 来完成这项工作,而我无法在 ViewModel 中进行异步调用。
Model类的代码是这样的:
public class ModelMail : INotifyPropertyChanged
{
//newly added
public Group Mail { get; set; }
public Group OutBox { get; set; }
public Group Draft { get; set; }
public Group SendItems { get; set; }
public bool IsDataLoaded { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
public void LoadData()
{
//Newly created
Mail = CreateMail();
OutBox = CreateOutBox();
Draft = CreateDraft();
SendItems = CreateSendItems();
IsDataLoaded = true;
}
private Group CreateDraft()
{
Group data = new Group();
data.Title = "all";
string[] gataFromDB = new string[] { "sample ", "sample", "sample", "sample" };
data.Items.Add(new Data { Name_1 = "Nisar Mohamed VM", Subject = "Subject Inbox sample mail subject Inbox sample mail subject Inbox sample mail subject Inbox sample mail subject Inbox sample mail subject 456", Message = "Message This is a sample mail message from nisar This is a sample mail message from nisar This is a sample mail message from nisar This is a sample mail message from nisar This is a sample mail message from nisarThis is a sample mail message from nisar", time = "11:00 AM", IsChecked = false, foreground = "Black", to = "gowthamrajs@hotmail.com", mailFullDateTime = "Fri 9/12 9:25 PM", from = "kkkk@knowledgeq.com" });
foreach (string dataa in gataFromDB)
{
data.Items.Add(new Data { Name_1 = "Nisar Mohamed VM", Subject = "Subject Inbox sample mail subject Inbox sample mail subject Inbox sample mail subject Inbox sample mail subject Inbox sample mail subject 456", Message = "Message This is a sample mail message from nisar This is a sample mail message from nisar This is a sample mail message from nisar This is a sample mail message from nisar This is a sample mail message from nisarThis is a sample mail message from nisar", time = "11:10 AM", IsChecked = false, foreground = "Black", to = "nisr199@hotmail.com", cc = "nisr19@gmail.com", mailFullDateTime = "Fri 9/12 9:25 PM", from = "kkkk@knowledgeq.com" });
}
return data;
}
private Group CreateOutBox()
{
Group data = new Group();
data.Title = "unread";
return data;
}
private Group CreateMail()
{
Group data = new Group();
data.Title = "all";
return data;
}
private Group CreateSendItems()
{
Group data = new Group();
data.Title = "all";
return data;
}
}
我该怎么做
【问题讨论】:
-
“我无法在 ViewModel 中进行异步调用。”这是一个要求吗?还是您使用的框架不支持异步等待?
-
谢谢先生,是的,您是正确的,我想在 CreateDraft() 或 CreateOutBox() 或 CreateSendItems() 等任何构造函数中使用异步,因为我需要使用 azure 数据库为这些构造函数设置值.
-
然后将它们标记为异步使用任务并使用
Task<T>例如private async Task<Group> CreateDraft() -
它在 Mail = CreateMail() 中显示“错误 2 无法将类型 'System.Threading.Tasks.Task
' 隐式转换为 'KQ_School.ViewModels.Group'”;我在哪里创建 constr ... -
你必须等待它
Mail = await CreateMail();`
标签: c# azure windows-phone-8 asynchronous mvvm