使用 powershell 和 windows 窗体将进度条插入状态栏面板

发布于 2024-08-16 05:00:21 字数 139 浏览 3 评论 0原文

我目前正在开发一个项目,需要我在状态栏的一个面板中放置一个进度条。有谁对此有任何经验,或者任何人都可以提供有关如何完成此操作的任何意见或指导。我已经寻找了两天的解决方案但没有运气。目前仍然没有大量的 powershell 帮助,尤其是在 Windows 窗体方面。

I am currently working on a project that requires me to put a progressbar in one of the panels of a statusbar. Does anyone have any experience with this, or can anyone provide any input or direction on how it is done. I have been searching for 2 days now for a solution with no luck. There is still not an abundance of powershell help out there, especially when it comes to windows forms.

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

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

发布评论

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

评论(1

无人问我粥可暖 2024-08-23 05:00:21

使用 WPK 中的 PowerShell 数据源来实现这一点相对简单。

您可以将 WPK 作为 PowerShellPack 的一部分获取。

这是一个用 WPK 编写的相当不错的进度查看器:

New-Grid -Columns 2 {
    New-TextBlock -Margin 10 -TextWrapping Wrap -ZIndex 1 -HorizontalAlignment Left -FontWeight Bold -FontSize 12 -DataBinding @{
        "Text" = "LastProgress.Activity"
    }
    New-TextBlock -Margin 10 -ZIndex 1 -TextWrapping Wrap -Column 1 -VerticalAlignment Bottom -HorizontalAlignment Right -FontStyle Italic -FontSize 12 -DataBinding @{
        "Text" = "LastProgress.StatusDescription"
    }
    New-ProgressBar -ColumnSpan 2 -MinHeight 250 -Name ProgressPercent -DataBinding @{
        "Value" = "LastProgress.PercentComplete"
    }
} -DataContext {
    Get-PowerShellDataSource -Script { 
        foreach ($n in 1..100) {
            Write-Progress "MajorProgress" "MinorProgress" -PercentComplete $n
            Start-Sleep -Milliseconds 250
        }
    }
} -AsJob

PowerShellDataSource 返回一个对象,其中包含给定流的所有输出以及给定流上输出的最后一项(即 Progress 和 LastProgress)。为了显示进度条,我们需要绑定到 LastProgress 属性。

代码的前半部分声明了进度条。通过使用 -DataBinding 参数,文本块和进度栏将自动同步到数据上下文。数据上下文可以在此级别声明(如示例所示),也可以在父控件中声明。

此示例中的 DataContext 是一个简单的 PowerShell 脚本,它使用 Write-Progress 每四分之一秒输出一条测试消息,但您可以使用您想要的任何脚本。

希望这有帮助。

This is relatively simple to do with PowerShell data sources in WPK.

You can get WPK as part of the PowerShellPack.

Here's a pretty decent progress viewer written in WPK:

New-Grid -Columns 2 {
    New-TextBlock -Margin 10 -TextWrapping Wrap -ZIndex 1 -HorizontalAlignment Left -FontWeight Bold -FontSize 12 -DataBinding @{
        "Text" = "LastProgress.Activity"
    }
    New-TextBlock -Margin 10 -ZIndex 1 -TextWrapping Wrap -Column 1 -VerticalAlignment Bottom -HorizontalAlignment Right -FontStyle Italic -FontSize 12 -DataBinding @{
        "Text" = "LastProgress.StatusDescription"
    }
    New-ProgressBar -ColumnSpan 2 -MinHeight 250 -Name ProgressPercent -DataBinding @{
        "Value" = "LastProgress.PercentComplete"
    }
} -DataContext {
    Get-PowerShellDataSource -Script { 
        foreach ($n in 1..100) {
            Write-Progress "MajorProgress" "MinorProgress" -PercentComplete $n
            Start-Sleep -Milliseconds 250
        }
    }
} -AsJob

The PowerShellDataSource returns an object with a list of all of the outputs for a given stream and the last item outputted on the given stream (i.e. Progress and LastProgress). In order to display the progress bar, we need bind to the LastProgress property.

The first half of the code declares the progress bar. By using the -DataBinding parameter the TextBlocks and Progress bars will automatically sync to the data context. Data context can either be declared at this level (as is shown in the example) or can be in a parent control.

The DataContext in this example is a simple PowerShell script that uses Write-Progress to output a test message every quarter second, but you can use any script you'd like.

Hope this helps.

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