【问题标题】:WPF ProgressBar & Dim Controls on OperationWPF ProgressBar & Dim 操作控件
【发布时间】:2011-02-21 20:45:34
【问题描述】:

我希望实现一个进度条(在我的应用程序状态栏上),同时在我执行时使除最小化/最大化/关闭窗口按钮之外的所有 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 参考)时,它没有显示任何进展——但是后台进程仍然工作。有什么建议吗?

【问题讨论】:

    标签: c# wpf progress-bar backgroundworker


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-25
      • 2011-07-27
      • 1970-01-01
      • 2011-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多