c#-正确显示BusyIndicator
作者:互联网
我有一个WPF C#应用程序.
我正在使用Xceeed.wpf工具包
用伪代码,我的函数将:
>显示忙碌指示器
>对我的服务器进行了REST API调用
> BusyIndicator停止显示
实际发生的是,直到REST API调用完成后,才显示BusyIndicator.有时确实会显示出来,但“标志性”影响并不是一帆风顺的.
我尝试了几种方法,这是我最近的尝试:
// raise event to invoke method call on main UI page
// event raised here
Task.Run(() =>
{
// makes API call
busyIndicator.IsBusy = false;
);
public void ShowBusy()
{
this.InvokeOnMainThread(() =>
{
busyIndicator.IsBusy = true;
});
}
public static void InvokeOnMainThread(this Control control, Action method)
{
if (!control.Dispatcher.CheckAccess())
{ Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, method);
return;
}
method();
}
标记:
<xctk:BusyIndicator Name="busyIndicator" Grid.Column="1" Grid.Row="1" IsBusy="True" DisplayAfter="0" Background="White" BorderBrush="White">
<xctk:BusyIndicator.BusyContentTemplate >
<DataTemplate>
<StackPanel Height="50">
<TextBlock HorizontalAlignment="Center">Please wait...</TextBlock>
<WPFSpark:FluidProgressBar Oscillate="True" Width="400" Foreground="DarkGreen" BorderBrush="White" Opacity="1" />
</StackPanel>
</DataTemplate>
</xctk:BusyIndicator.BusyContentTemplate>
<xctk:BusyIndicator.OverlayStyle>
<Style TargetType="Rectangle">
<Setter Property="Fill" Value="White"/>
</Style>
</xctk:BusyIndicator.OverlayStyle>
<xctk:BusyIndicator.ProgressBarStyle>
<Style TargetType="ProgressBar">
<Setter Property="BorderBrush" Value="white"></Setter>
<Setter Property="Visibility" Value="Collapsed"/>
</Style>
</xctk:BusyIndicator.ProgressBarStyle>
<xctk:BusyIndicator.Content>
//my usercontrol which invokes the call
</xctk:BusyIndicator.Content>
</xctk:BusyIndicator>
解决方法:
您必须等到任务完成后才能关闭BusyIndicator.目前,您只是启动任务并将忙碌指示灯设置为熄灭.
将您的方法标记为异步并等待API调用.
busyIndicator.IsBusy = true;
await Task.Run(() =>
{
//makes api call
);
busyIndicator.IsBusy = false;
如果由于某种原因,您不能使用async / await,那么请使用延续.
Task.Run(() =>
{
//makes api call
}).ContinueWith(antecedent =>
{
busyIndicator.IsBusy = false;
}, TaskScheduler.FromCurrentSynchronizationContext());
标签:xceed,wpf,c 来源: https://codeday.me/bug/20191119/2033123.html