2014-10-03 56 views
0

我正在开发使用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; 
    } 

} 

我怎样才能

+0

“我无法在ViewModel中进行异步调用。”这是一个要求吗?或者你在使用不支持异步等待的框架? – 2014-10-03 06:02:39

+0

谢谢先生,是的,你是正确的,我想在CreateDraft()或CreateOutBox()或CreateSendItems()等任何构造函数中使用异步,因为我需要为使用Azure数据库的构造函数设置值。 – user2798846 2014-10-03 06:07:50

+0

然后将它们标记为异步使用任务并使用“任务”例如私人异步任务 CreateDraft()' – 2014-10-03 06:12:48

回答

3

你会发现我的MSDN article on async data binding很有帮助。总之,如果你是数据绑定,那么当数据下载完成时你需要提升PropertyChangedTask没有执行INotifyPropertyChanged,所以需要一些帮助。在我的文章中,我有一个类型NotifyTaskCompletion<T>,它可以用作异步操作的数据绑定包装器。

你会在你的构造函数启动异步操作,并创建一个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中需要一些额外的状态。具体而言,操作正在进行时的“加载”状态;以及操作异步失败时的“错误”状态。