【问题标题】:Using WPK for PowerShell GUI为 PowerShell GUI 使用 WPK
【发布时间】:2011-07-12 06:46:28
【问题描述】:

我们创建了一些很棒的 PowerShell 脚本。但随着时间的推移,我们将脚本提供给了非程序员。所以我们决定是时候为 PowerShell 提供一个简单易懂的 GUI。

我看过 James Brundage 的第 9 频道视频。他的视频很好地解释了窗口、堆栈面板、文档面板、网格、标签和文本框。 但是,总共有 60 多个控件。不知道 WPF 没有帮助。

我想做的是能够做到以下几点:

文本输入
下拉列表
单选按钮组
复选框组

我想出了一个可以完成前三个但有错误的示例。
Radio 控件直观地显示选择的第一个按钮,但是当我拉取组的值时它不正确。

我找不到复选框的示例。

任何提示将不胜感激。

这是我在 PowerShell 中对 WPK 的测试:

function TestGui {
    Import-Module WPK

    $SelectedRadio = "First"

    New-Window -Title "Test User Input" -WindowStartupLocation CenterScreen `
      -Width 400 -Height 300 -Show {

      New-Grid -Rows 32*, 32*, 32*, 32* -Columns 100, 1* {

        #create style to use on controls
        $createLblStyle = @{
            Margin = 5
            HorizontalAlignment = "right"
            VerticalAlignment = "center"
        } 

        #Label Text for this row
        New-TextBlock -Text "Pick fruit" `
          -Row 0 -Column 0 @createLblStyle

        # dropdown ( combo box)
        New-ComboBox -Name FruitList `
          -row 0 -column 1 @createLblStyle `
          -Items { "Apple", "Pear", "Peach" } -SelectedIndex 0

        #Label Text for this row
        New-TextBlock -Text "Pick number" `
            -Row 1 -Column 0 @createLblStyle

        # TextBox
        New-TextBox -Name TextInputName `
          -Row 1 -Column 1 @createLblStyle 

        #Label Text for this row
        New-TextBlock -Text "Get Text Input" `
          -Row 2 -Column 0 @createLblStyle        

        #Do three radio buttons for this row.
        New-StackPanel -Row 2 -Column 1 -Orientation Horizontal {
            New-RadioButton -Content "Pick first" `
                -GroupName Results -IsChecked $True -On_Click {
                    $SelectedRadio = "First"
                }

            New-RadioButton -Content "Pick two" `
                -GroupName Results  -On_Click {
                    $SelectedRadio = "Second"
                }

            New-RadioButton -Content "Pick three" `
                -GroupName Results   -On_Click {
                    $SelectedRadio = "Third"
                }
        }


        New-Button -Content "_Call PS Script" -Row 3 -Column 0 -Margin 3 -On_Click {
          $FruitList          = $window | Get-ChildControl FruitList
          $TextInputName      = $Window | Get-ChildControl TextInputName
          $Results            = $Window | Get-ChildControl Results

          $Window.Close()
          write-host "call PS script with: "
          write-host "DropDown => " $FruitList.SelectedValue
          write-host "TextBox => " $TextInputName.Text
          write-host "Radio => " $SelectedRadio
    }

        New-Button -Content "Cancel" -Row 3 -Column 1 -Margin 3 -On_Click {
          $Window.Close()
          write-host "Cancel was pressed"
        }
      }
    }
}

【问题讨论】:

  • 单选按钮有什么问题?我什么都看不到。
  • bernd,当我尝试取回选定的按钮时,它并没有给我我选择的那个。
  • 我测试了所有 e 案例的初始状态,中间,右侧,左侧单选按钮和“调用 PS 脚本”给出: Radio => First, Radio => First, Radio => Third, Radio => First .您是否始终获得相同的值,还是获得不同但错误的值?
  • 我相信无论我检查什么,我都只得到了第一个值。我将不得不重新审视这段代码,因为我使用了一个下拉列表来解决这个问题。虽然如果可以的话,我希望能够使用收音机。

标签: user-interface powershell


【解决方案1】:

据我了解,您希望在您编写的 PowerShell 脚本之上创建一个 GUI。

我的看法是你有两种技术解决方案。

  1. 您在 Windows 窗体顶部构建 GUI,这是在 de .NET Framework 中构建 GUI 的前一种方式
  2. 您在 WPF 之上构建一个 GUI,这是在 de .NET Framework 中构建 GUI 的一种新方法

在第一个解决方案中,您可以直接使用 Visual Studio 和“transform”创建表单,或者使用名为“PrimalForms Community Edition”的 Sapien 公司工具创建它,这是一个免费的 GUI 构建器工具,适用于 PowerShell 用户.您构建您的 GUI(如 .NET 开发人员)并且该工具以 PowerShell 语法生成窗口窗体代码。您只需在正确的位置调用脚本函数(为每个事件脚本块生成的函数)。此解决方案适用于正常形式。

WPF 解决方案较新,专门用于在表单中添加 2D 或 3D 图形或适应不同屏幕尺寸的情况。在此解决方案中,您必须构建表单的 XAML 表示形式(例如使用 Visual Studio 2010 Expess),然后将其加载到 PowerShell 中。此解决方案需要安装 PowerShell 2.0 和 Framework 3.5 以及特殊标志 (-STA) 才能启动 PowerShell。对我来说,WPK 模块是在这个解决方案上构建的。

它还有一种在 PowerShell 中创建图表的方法。

如果您对其中一种或其他解决方案感兴趣,请告诉我。

希望对你有帮助。

日本

【讨论】:

  • JP,我正在尝试使用 PowerShellPack (WPK) 而不是通过创建 Windows 窗体来专门执行此操作。例如:运行一个脚本,该脚本会打开一个小窗口以进行一些简单的输入。使用一个按钮调用更多脚本以使用提供的输入。我可以使其与 New-TextBox / New-ComboBox 一起使用(通过手动添加项目),但我仍在尝试查找 New-RadioButton(List)/New-CheckBox(List) 的语法示例,并且可能是 ListBox 而不是 ComboBox。
【解决方案2】:

在单选按钮的On_Click 脚本块中更改为$Script:SelectedRadio

【讨论】:

    【解决方案3】:

    要找出您的三个单选按钮中的哪一个已被选中,您可以使用以下表达式:

    if ( ($Window | Get-ChildControl 'One' ).IsChecked ) {
       $SelectedRadioButton = '1'
    } elseif ( ($Window | Get-ChildControl 'Two' ).IsChecked ) {
       $SelectedRadioButton = '2'
    } elseif ( ($Window | Get-ChildControl 'Three' ).IsChecked) {
       $SelectedRadioButton = '3'
    }
    

    如果您只有一组单选按钮,则无需指定组名。也不需要 On_Click 事件。您可以将 New-RadioButton 表达式缩短为:

    New-RadioButton -Name 'One'   -Content "Pick first" -IsChecked $true
    New-RadioButton -Name 'Two'   -Content "Pick two"
    New-RadioButton -Name 'Three' -Content "Pick three"
    

    要查找,是否有复选框:

    New-CheckBox -Name 'Uno' -Content "Uno"
    

    已经检查过了,你也可以推断出它的 IsChecked 属性:

    Write-Host "Checkbox UNo => " ($Window | Get-ChildControl UNo).IsChecked
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-29
      • 2018-10-17
      • 2016-01-28
      • 2018-09-27
      • 1970-01-01
      相关资源
      最近更新 更多