我有一个WPF C#应用程序。
我正在使用Xceeed.wpf工具包
在伪代码中,我的函数将:
实际发生的情况是,在REST调用完成之前,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>发布于 2016-02-11 13:40:50
您必须等到Task完成后才能设置BusyIndicator。此时,您只需启动任务并将繁忙的指示器设置为“关闭”。
标记您的方法异步并等待API调用。
busyIndicator.IsBusy = true;
await Task.Run(() =>
{
//makes api call
);
busyIndicator.IsBusy = false;如果..。由于某些原因,您不能使用异步/等待,然后使用延续。
Task.Run(() =>
{
//makes api call
}).ContinueWith(antecedent =>
{
busyIndicator.IsBusy = false;
}, TaskScheduler.FromCurrentSynchronizationContext());https://stackoverflow.com/questions/35340682
复制相似问题