我的代码
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 :(得分:2)
正如人们在评论中所解释的那样,问题可能是您的“魔术代码”可能是同步的,并阻止整个UI。你想要做的是使这个调用异步。
执行此操作的一种简单方法是在同步代码周围调用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");
}
}