2015-08-03 119 views
-1

我的代碼進度對話框不顯示

protected async Task SyncAll() 
{ 
    var ProgressAlert = await this.ShowProgressAsync("Please wait...", "Sync...."); //show message 
    ProgressAlert.SetIndeterminate(); //Infinite 

    try 
    { 
     //some magic code here 
     //show info 
     await ProgressAlert.CloseAsync(); 
     await this.ShowMessageAsync("End","Succes!"); 
    } 
    catch 
    { 
     await ProgressAlert.CloseAsync(); 
     await this.ShowMessageAsync("Error!", "Contact with support"); 
    } 

} 

private async void SyncButton_Click(object sender, RoutedEventArgs e) 
{ 
    await SyncAll(); 
} 

,我只收到一個暗淡的窗口並沒有ProgressDialog。 我想執行我的代碼,並用ProgressDialog實例操縱他。

我做錯了什麼?

+0

你想做什麼? 你能寫英文的messagebox文字嗎?所以我們可以很好理解... – ghiboz

+0

這沒關係,我想顯示ProgressDialog,在try塊中做一些事情,並隱藏ProgressDialog。 – user3468055

+0

「幻碼」是不是阻塞(即正確使用)? – thumbmunkeys

回答

2

正如人們在評論中解釋的那樣,問題可能是您的「魔術代碼」可能是同步的,並會阻止整個用戶界面。你想要做的是使這個調用異步。

一個簡單的方法就是在你的同步代碼周圍調用Task.Run

比方說,你把你的「魔法代碼」放到一個名爲MyMagicCode()的方法中。

protected async Task SyncAll() 
{ 
    var ProgressAlert = await this.ShowProgressAsync("Please wait...", "Sync...."); //show message 
    ProgressAlert.SetIndeterminate(); //Infinite 

    try 
    { 
     await Task.Run(() => MyMagicCode()); 

     //show info 
     await ProgressAlert.CloseAsync(); 
     await this.ShowMessageAsync("End","Succes!"); 
    } 
    catch 
    { 
     await ProgressAlert.CloseAsync(); 
     await this.ShowMessageAsync("Error!", "Contact with support"); 
    } 

}