WPF ProgressBar&操作昏暗控制

时间:2011-02-21 20:45:35

标签: c# wpf progress-bar backgroundworker

我正在寻找实现进度条(在我的应用程序状态栏上),同时在执行时调暗除最小化/最大化/关闭窗口按钮之外的所有MainWindow控件。

现在,我有一个带有后台工作进程的进度条用户控件,但它会在一个单独的窗口中弹出进度条控件。这是状态栏的XAML:

StatusBar x:Name="StatusB" Grid.Column="0" Grid.Row="5" Width="Auto" Height="30" Background="DarkGray" DockPanel.Dock="Bottom">
            <StackPanel>
                <Label Content="Status:" Foreground="White" Height="30" Width="50" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" ></Label>
            </StackPanel>
            <StackPanel>
                <local:ProgressB></local:ProgressB>
            </StackPanel>
        </StatusBar>

这是我的进度条用户控件:

<Grid Width="250" Height="25">
    <Label x:Name="lblProgress" Content="0%" Width="25" Height="20" Margin="155,2,70,0" Foreground="White" IsEnabled="False" Visibility="Hidden"></Label>
    <ProgressBar x:Name="progress" Height="20" IsIndeterminate="False" Width="151" HorizontalAlignment="Left" Margin="0,2,0,0"></ProgressBar>
    <Button x:Name="btnCancel" Width="50" Height="20" HorizontalAlignment="Right" Click="btnCancel_Click" Content="Cancel" Margin="0,2,0,0" IsEnabled="False" Visibility="Hidden"></Button>
</Grid>

后台工作人员:

        private void ExecuteBuild(object sender, ExecutedRoutedEventArgs e)
    {
        //Launching the bind/train process requires a separate proc
        ProcessStartInfo startInfo = new ProcessStartInfo("app.exe", filePath);
        startInfo.CreateNoWindow = true;
        startInfo.UseShellExecute = false;
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.RedirectStandardError = true;
        startInfo.RedirectStandardOutput = true;

        // Borrowed for build iterations (you need 3, minimum)
        int maxRecords = 3;

        pd = new ProgressB();

        // Cancel Build 
        pd.Cancel += CancelProcess;

        // Add multithread dispatcher
        System.Windows.Threading.Dispatcher pdDispatcher = pd.Dispatcher;

        //create our background worker and also have a cancel button
        worker = new BackgroundWorker();
        worker.WorkerSupportsCancellation = true;

        worker.DoWork += delegate(object s, DoWorkEventArgs args)
        {
            // Updates the progress text
            UpdateProgressDelegate update = new UpdateProgressDelegate(UpdateProgressText);
            int x = 0;

            for (x = 1; x <= maxRecords; x++)
            {
                if (worker.CancellationPending)
                {
                    args.Cancel = true;
                    return;
                }

                System.Threading.Thread.Sleep(1);

当我让它弹出一个对话框窗口时,它工作正常但是当我把它放在我的状态栏中时(参见第一个XAML参考)它没有显示任何进展 - 但后台进程仍然有效。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您没有看到更新的原因可能是UI线程上没有调用UpdateProgress。您的代码示例被截断,因此很难说。使用BackgroundWorker.ReportProgress报告进度。这将在UI线程上调用。 一些样本here