powershell PowerShell GUI的基本模板

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell PowerShell GUI的基本模板相关的知识,希望对你有一定的参考价值。

#ERASE ALL THIS AND PUT XAML BELOW between the @" "@ 
$inputXML = @"
<Window x:Name="Diag_V" x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Diag_V"
        mc:Ignorable="d"
        Title="Diag-V" Height="350" Width="525">
    <Grid>
        <ComboBox x:Name="cboChoices" HorizontalAlignment="Left" Margin="176,100,0,0" VerticalAlignment="Top" Width="120">
            <ComboBoxItem Content="X"/>
            <ComboBoxItem Content="Y"/>
            <ComboBoxItem Content="Z"/>
        </ComboBox>
        <TextBlock x:Name="txtResults" HorizontalAlignment="Left" Margin="151,169,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="71" Width="162"/>
    </Grid>
</Window>
"@       
 
$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N'  -replace '^<Win.*', '<Window'
 
 
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = $inputXML
#Read XAML
 
    $reader=(New-Object System.Xml.XmlNodeReader $xaml) 
  try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."}
 
#===========================================================================
# Load XAML Objects In PowerShell
#===========================================================================
 
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)}
 
Function Get-FormVariables{
if ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true}
write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
get-variable WPF*
}
 
Get-FormVariables
 
#===========================================================================
# Actually make the objects work
#===========================================================================
 
$WPFcboChoices.Add_DropDownClosed({
    $choice = $WPFcboChoices.Text
    $WPFtxtResults.text = "You selected $choice"
})

#Sample entry of how to add data to a field
 
#$vmpicklistView.items.Add([pscustomobject]@{'VMName'=($_).Name;Status=$_.Status;Other="Yes"})
 
#===========================================================================
# Shows the form
#===========================================================================
write-host "To show the form, run the following" -ForegroundColor Cyan
'$Form.ShowDialog() | out-null'

$Form.ShowDialog() | out-null

以上是关于powershell PowerShell GUI的基本模板的主要内容,如果未能解决你的问题,请参考以下文章

将 Powershell 变量的值获取到 Python (wxGlade) GUI

从 Start-Job (Powershell) 更新 winforms GUI

使用Powershell添加网络打印机(带GUI界面)

是否可以捕获从 IIS 等 Windows GUI 生成的 PowerShell 命令?

在Powershell中更新GUI

使用WPF为Powershell程序制作GUI界面