我正在尝试升级以前基于 BackgroundWorker 的方法,以使用Microsoft.Bcl.Async库中的异步调用。我遇到的麻烦是繁忙的指示器不再适用于新的异步功能(我是新手)。忙碌指示符不显示,GUI 在操作执行时冻结,然后在操作完成后解除冻结。我不确定我做错了什么......
XAML 代码:
<Border BorderBrush="Black" BorderThickness="1" CornerRadius="5,5,15,15" Background="#FFA8A9AC" Grid.Column="1" Grid.Row="1">
<Grid x:Name="UIGrid">
<toolkit:BusyIndicator x:Name="BusyIndicator" IsBusy="{Binding Instance.IsBusy}" BusyContent="{Binding Instance.BusyMessage, TargetNullValue='Please wait...'}" >
<Grid x:Name="ScreenGrid" />
</toolkit:BusyIndicator>
</Grid>
</Border>
代码
// Button click handler.
private async void NewReportClick(object sender, RoutedEventArgs e)
{
SystemLog.Trace(this);
await this.CreateReport();
}
// Async implementation.
private async Task CreateReport()
{
SystemLog.Trace(this);
await this.CloseReport();
BusinessLogic.Instance.BusyMessage = "CREATING new report...";
BusinessLogic.Instance.IsBusy = true;
await BusinessLogic.Instance.ReportManager.CreateReport();
BusinessLogic.Instance.IsBusy = false;
BusinessLogic.Instance.BusyMessage = null;
}
编辑:回复@Jon Skeet,这里是 CreateReport 的重要部分:
public async Task CreateReport()
{
SystemLog.Trace(this);
var internalReportId = GenerateReportId();
var rpt = await ReportFactory.Create();
rpt.InternalReportId = internalReportId;
rpt.EnableNotifications = true;
ReportRepository = new ReportRepository();
await SaveReport(rpt);
// Set the active report.
BusinessLogic.Instance.ActiveReport = rpt;
await AssignReportNumber(MediViewData.Instance.ActiveReport);
}
public async static Task<Report> Create()
{
var rpt = new Report();
/** Set several fields to default values. */
return rpt;
}