powershell WPF与PowerShell

Posted

tags:

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

# Originally script: https://blogs.technet.microsoft.com/heyscriptingguy/2014/08/01/ive-got-a-powershell-secret-adding-a-gui-to-scripts/
# I've replaced the relative paths
# Directory structure:
#
# <Project_Name>    <= what you want :)
#       |
#       +-- Forms
#       |     |
#       |     +-- MyForm.xaml
#       |
#       +-- Scripts
#             |
#             +-- loadDialog.ps1
#             |
#             +-- HelloWorld.ps1


# ===============================================================
## <Project_Name>\Forms\MyForm.xaml
# ===============================================================

  <Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
      <Label Name="Label1" Content="Label" HorizontalAlignment="Left" Margin="68,38,0,0" VerticalAlignment="Top" Width="197"/>
      <Button Name="Button1" Content="Button" HorizontalAlignment="Left" Margin="307,41,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
  </Window>

# ===============================================================
## <Project_Name>\Scripts\loadDialog.ps1
# ===============================================================

  [CmdletBinding()]
  Param(
    [Parameter(Mandatory=$True,Position=1)]
    [string]$XamlPath
  )

  [xml]$Global:xmlWPF = Get-Content -Path $XamlPath

  #Add WPF and Windows Forms assemblies
  try{
    Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,system.windows.forms
  } catch {
    Throw "Failed to load Windows Presentation Framework assemblies."
  }

  #Create the XAML reader using a new XML node reader
  $Global:xamGUI = [Windows.Markup.XamlReader]::Load((new-object System.Xml.XmlNodeReader $xmlWPF))

  #Create hooks to each named object in the XAML
  $xmlWPF.SelectNodes("//*[@Name]") | %{
    Set-Variable -Name ($_.Name) -Value $xamGUI.FindName($_.Name) -Scope Global
  }

# ===============================================================
## <Project_Name>\Scripts\HelloWorld.ps1
# ===============================================================

  <#
  .SYNOPSIS
  Used to Demostrate Creating a GUI for PowerShell

  .DESCRIPTION
  Be sure to have the three files, your XAML Form, the loadDialog.ps1 helper cmdLet, and this file.  For clean separation
  Store your .ps1 files in a Scripts Folder and the XAML forms in a Forms folder
  #>

  $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  $parentDir = (Get-Item $scriptDir).Parent.FullName

  & "$scriptDir\loadDialog.ps1" -XamlPath "$parentDir\Forms\MyForm.xaml"

  #EVENT Handler 
  $button1.add_Click({  
  	$Label1.Content = "Hello World"
  })

  #launch the window
  $xamGUI.ShowDialog() | out-null

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

PowerShell\WPF:使用数据模板的 PowerShell 中的空单选按钮对象

WPF Datagrid Combobox SelectedItem 未正确绑定到 Powershell 对象

WPF+PowerShell制作单机版应用

powershell WPF ClickOnce应用程序的Octopus部署脚本示例。

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

如何通过Powershell代码更改特定WPF列表框项的背景颜色?