我正在開發使用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