WPF 进度条 &操作时的昏暗控制

发布于 2024-10-18 14:16:51 字数 2853 浏览 2 评论 0原文

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

现在,我有一个带有后台工作进程的进度条用户控件,但它会在单独的窗口中弹出进度条控件。这是我的状态栏的 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 参考),它显示没有进度不管怎样——但是后台进程仍然在工作。有什么建议吗?

I'm looking to implement a progress bar (on my applications status bar) and at the same time dim all the MainWindow controls except minimize/maximize/close window buttons when I execution.

Right now, I have a Progress Bar User Control with a Background worker process but it pops the progress bar control in a separate window. Here is the XAML for my status bar:

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>

Here is my Progress Bar User Control:

<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>

Background Worker:

        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);

When I have it pop a dialog window it works fine but when I put it in my status bar (see first XAML ref) it shows no progress whatsoever -- however the background process does work still. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦情居士 2024-10-25 14:16:51

您看不到更新的原因可能是 UI 线程上未调用 UpdateProgress。你的代码示例被切断了,所以很难说。使用BackgroundWorker.ReportProgress 报告进度。这将在 UI 线程上调用。
此处提供一些示例。

The reason you're not seeing updates is probably that UpdateProgress is not called on the UI thread. Your code sample is cut off, so it's hard to say. Use BackgroundWorker.ReportProgress for reporting progress. This will be called on the UI thread.
Some samples here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文