进度对话框没有显示

本文关键字:显示 对话框 | 更新日期: 2023-09-27 18:06:01

我的代码

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实例执行我的代码并在他内部操作。

我做错了什么?

进度对话框没有显示

正如人们在评论中解释的那样,问题可能是你的"Magic代码"可能是同步的,并且阻塞了整个UI。你要做的是使这个调用异步化。

一个简单的方法是在同步代码周围调用Task.Run

假设您将"Magic Code"放入一个名为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");
    }
}