简体   繁体   English

WPF扩展工具包BusyIndi​​cator在运行期间未显示

[英]Wpf Extended toolkit BusyIndicator not showing during operation

I have a fairly long operation that occurs in a background worker. 我在后台工作人员中发生了相当长的操作。 I have the busy indicator set to true before the operation and false when the operation ends, but the indicator never shows. 在操作之前,我将忙碌指示器设置为true,在操作结束时将其设置为false,但是指示器从不显示。 It shows in other parts of my program, but not this one. 它显示在我程序的其他部分,但没有显示。 heres the code: 这是代码:

<xctk:BusyIndicator DockPanel.Dock="Top" Name="ItemSearchBusyIndicator" >
<Grid Height="35" Name="grid1" Width="445" DockPanel.Dock="Top">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="382*" />
        <ColumnDefinition Width="39*" />
    </Grid.ColumnDefinitions>
    <RadioButton Content="Direct Entry" Height="16" HorizontalAlignment="Left" Margin="212,10,0,0" Name="RdoDirectEntry" VerticalAlignment="Top" IsChecked="False" Checked="RdoDirectEntry_Checked" FontSize="14" />
    <RadioButton Content="Item Search" Height="16" HorizontalAlignment="Left" Margin="0,10,0,0" Name="RdoItemSearch" VerticalAlignment="Top" Checked="RdoItemSearch_Checked" FontSize="14" />
    <RadioButton Content="Route Search" Height="16" HorizontalAlignment="Left" Margin="102,10,0,0" Name="RdoRouteSearch" VerticalAlignment="Top" Checked="RdoRouteSearch_Checked" FontSize="14" />
    <RadioButton Content="File System Search" Height="16" HorizontalAlignment="Left" Margin="310,11,0,0" Name="RdoFileSystem" VerticalAlignment="Top" FontSize="14" Grid.ColumnSpan="2" Checked="RdoFileSystem_Checked" />
</Grid>
</xctk:BusyIndicator>

        private void CboItemId_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        BackgroundWorker _backgroundWorker = new BackgroundWorker();
        _backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
        _backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_backgroundWorker_RunWorkerCompleted);

      ItemSearchBusyIndicator.IsBusy = true;
       // Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
        if (RdoItemSearch.IsChecked == false) return;
        backgroundWorker_DoWork(null, null);
       // Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
       ItemSearchBusyIndicator.IsBusy = false;
    }

    public void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        if (CboItemId.SelectedValue == null) return;
        if (CboItemId.SelectedValue.ToString() != string.Empty)
            LoadItemData(CboItemId.SelectedValue.ToString());
    }
      public void LoadItemData(string itemId)
    {
        Axapta ax = new Axapta();
        files.Clear();
        try
        {
            ax.Logon(Settings.Default.Server, null, Settings.Default.Test, null);
            AxaptaContainer path = (AxaptaContainer)ax.CallStaticClassMethod(Settings.Default.ClassName, Settings.Default.ItemData, itemId);
            for (int i = 1; i <= path.Count; i++)
            {
                AxaptaContainer somestring = (AxaptaContainer)path.get_Item(i);
                for (int j = 1; j <= somestring.Count; j += 2)
                {
                    string extension = Path.GetExtension(somestring.get_Item(j + 1).ToString().ToLower());
                    if (extension == ".jpg"
                        || extension == ".jpeg"
                        || extension == ".gif"
                        || extension == ".png"
                        || extension == ".bmp"
                        || extension == ".pdf")
                        /* key=path - value=description */
                        files.Add(somestring.get_Item(j + 1).ToString(), somestring.get_Item(j).ToString());
                }
            }

            if (path.Count == 0)
            {
                MessageBox.Show("No Documents Found");
            }
            _canvas.Children.Clear();
            LoadPictures();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            ax.Logoff();
        }
    }

The mouse override in CboItemId_SelectionChanged works fine when i use it, but i'd rather use the busy indicator. 当我使用CboItemId_SelectionChanged中的鼠标替代时,它工作正常,但我宁愿使用忙碌指示器。 the indicator does pop up AFTER the operation if i just set it to true, and never set it to false. 如果我只是将其设置为true,并且从不将其设置为false,则该指示符会在操作后弹出。

Your "background" code isn't background at all. 您的“背景”代码根本不是背景。 There must be something like this: 必须有这样的东西:

private void CboItemId_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // turn indicator ON
    ItemSearchBusyIndicator.IsBusy = true;

    var _backgroundWorker = new BackgroundWorker();
    _backgroundWorker.DoWork += backgroundWorker_DoWork;
    _backgroundWorker.RunWorkerCompleted += _backgroundWorker_RunWorkerCompleted;

    // start background operation;
    // direct call to backgroundWorker_DoWork just calls method synchronously
    _backgroundWorker.RunWorkerAsync();
}

private void _backgroundWorker_RunWorkerCompleted(/* I don't remember signature */)
{
    // turn indicator OFF
    ItemSearchBusyIndicator.IsBusy = false;

    // other code
}

Also, note, that BackgroundWorker is rather obsolete API. 另外,请注意, BackgroundWorker是过时的API。 Consider TPL (and, optionally, async/await) instead. 考虑使用TPL (以及可选的异步/等待)。

在按钮单击上实现BusyIndi​​cator的另一种新方法: https ://brianlagunas.com/extended-wpf-toolkitusing-the-busyindicator/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM