我試圖升級基於BackgroundWorker的方法以使用來自Microsoft.Bcl.Async庫的異步調用。我遇到的麻煩是繁忙的指標不再適用於新的異步功能(我是新來的)。繁忙指示符不顯示,GUI執行時凍結,然後在完成時解凍。我不知道我做錯了什麼......BusyIndicator不從異步方法顯示
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;
}
編輯: 要回復@喬恩飛碟雙向,這裏是重要的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;
}
ReportManager.CreateReport是做什麼的?如果這只是同步執行,那麼它不會幫助你... – 2013-04-25 22:21:08
我已經添加了一些代碼,希望澄清它在做什麼。有什麼東西突出是錯的嗎? – dythim 2013-04-25 22:44:59
那麼哪個位實際需要時間?它是「SaveReport」嗎? * *看起來像什麼?基本上,我懷疑你實際上在做同步調用的大部分工作。 – 2013-04-25 22:51:48