我正在使用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 apis使用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;
}
}
我怎么能
答案 0 :(得分:3)
您可能会发现我的MSDN article on async data binding有帮助。总之,如果您是数据绑定,则需要在下载数据完成时引发PropertyChanged
。 Task
未实现INotifyPropertyChanged
,因此需要一些帮助。我的文章中有一个类型NotifyTaskCompletion<T>
,可以用作异步操作的数据绑定包装。
您将在构造函数中启动异步操作,并创建一个包含(data-bindable)结果的NotifyTaskCompletion<T>
:
public class MyViewModel
{
public MyViewModel()
{
Mail = new NotifyTaskCompletion<Group>(CreateMailAsync());
}
public NotifyTaskCompletion<Group> Mail { get; private set; }
private async Task<Group> CreateMailAsync()
{
// Azure calls go here
}
}
然后,您的数据绑定代码可以使用NotifyTaskCompletion<T>
上的属性来更新UI:
<Grid>
<!-- Busy indicator -->
<Label Content="Loading..." Visibility="{Binding Mail.IsNotCompleted, Converter={StaticResource BooleanToVisibilityConverter}}"/>
<!-- Results -->
<Label Content="{Binding Mail.Result.Title}" Visibility="{Binding Mail.IsSuccessfullyCompleted, Converter={StaticResource BooleanToVisibilityConverter}}"/>
<!-- Error details -->
<Label Content="{Binding Mail.ErrorMessage}" Background="Red" Visibility="{Binding Mail.IsFaulted, Converter={StaticResource BooleanToVisibilityConverter}}"/>
</Grid>
请注意,您的应用程序UI中需要一些额外的状态。具体而言,操作正在进行时的“加载”状态;操作异步失败时的“错误”状态。