首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正确显示BusyIndicator

正确显示BusyIndicator
EN

Stack Overflow用户
提问于 2016-02-11 13:30:17
回答 1查看 5.5K关注 0票数 4

我有一个WPF C#应用程序。

我正在使用Xceeed.wpf工具包

在伪代码中,我的函数将:

  • 显示BusyIndicator
  • 对我的服务器进行REST调用。
  • BusyIndicator停止显示

实际发生的情况是,在REST调用完成之前,BusyIndicator不会显示。有时它确实显示,但‘品牌’的影响是不流动的。

我尝试过几种方法,这是我最近的尝试:

代码语言:javascript
复制
// 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();
}

标记:

代码语言:javascript
复制
    <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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-11 13:40:50

您必须等到Task完成后才能设置BusyIndicator。此时,您只需启动任务并将繁忙的指示器设置为“关闭”。

标记您的方法异步并等待API调用。

代码语言:javascript
复制
busyIndicator.IsBusy = true;
await Task.Run(() =>
{
    //makes api call
);
busyIndicator.IsBusy = false;

如果..。由于某些原因,您不能使用异步/等待,然后使用延续。

代码语言:javascript
复制
  Task.Run(() =>
  {
      //makes api call
  }).ContinueWith(antecedent =>
  {
      busyIndicator.IsBusy = false;
  }, TaskScheduler.FromCurrentSynchronizationContext());
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35340682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档