我正在开发使用mvvm概念(数据,组,模型)win win phone 8应用程序,我完成了我的应用程序设计使用这个概念。现在我正尝试将我的应用程序连接到Azure DB,我还通过以下代码连接了使用MVVM概念的Azure DB,并且它的工作成功。MVVM异步调用viewmodel:我怎样才能连接天空数据库与MVVM
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 apis使用async/await来完成这项工作,而且我无法在ViewModel中进行异步调用。
的代码是这样的模型类:
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 = "[email protected].com", mailFullDateTime = "Fri 9/12 9:25 PM", from = "[email protected]" });
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 = "[email protected]", cc = "[email protected]", mailFullDateTime = "Fri 9/12 9:25 PM", from = "[email protected]" });
}
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中进行异步调用。”这是一个要求吗?或者你在使用不支持异步等待的框架? – 2014-10-03 06:02:39
谢谢先生,是的,你是正确的,我想在CreateDraft()或CreateOutBox()或CreateSendItems()等任何构造函数中使用异步,因为我需要为使用Azure数据库的构造函数设置值。 – user2798846 2014-10-03 06:07:50
然后将它们标记为异步使用任务并使用“任务”例如私人异步任务 CreateDraft()' –
2014-10-03 06:12:48