2014-10-03 85 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中需要一些額外的狀態。具體而言,操作正在進行時的「加載」狀態;以及操作異步失敗時的「錯誤」狀態。