WPF+PowerShell製做單機版應用

我接觸到的PowerShell GUI化工具製做有三種方式
一.Web-------------B/S架構
二.WPF------------C/S架構
三.WinForm-------C/S架構shell

一.Web
詳見個人另外一篇 http://www.javashuo.com/article/p-wzatxyul-bg.html express

二.WPF
WPF介紹,能夠參考 http://liutiemeng.blog.51cto.com/120361/91631/
簡單說就是用來作GUI的,用的XAML,支持數據綁定。而XAML本質上就是XML,
PowerShell能夠很方便的操做XML對象,這樣就能實現PowerShell和WPF結合。國外有很多這方面的帖子,不過不少都比較老,再也不更新了。
https://foxdeploy.com/series/learning-gui-toolmaking-series/ 上有很詳細的PowerShell+WPF的說明和介紹。最近的更新還加入了多線程。裏面也有WinForm的教程。
簡單總結下就是:
1.在vs studio下用XAML作好GUI界面。多線程

2.把XAML代碼直接粘貼到下面PowerShell代碼的@""@裏,或者XAML另存爲一個文件,而後在PowerShell代碼裏用Get-Content加載進來也能夠。架構

3.下面是用PowerShell操做XAML的代碼,foxdeploy已經寫好的模板,直接拿來用就行。這裏面沒寫事件控制,具體的看foxdeploy博客吧,和WinForm的同樣。ide

#ERASE ALL THIS AND PUT XAML BELOW between the @" "@
$inputXML = @"
<Window x:Class="Azure.Window1" 
    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:Azure" 
    mc:Ignorable="d" 
    Title="iVision Azure Accelerator" 
    Height="524.256" Width="332.076">
    <Grid Margin="0,0,174,0">
    </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-Warning "Unable to parse XML, with error: $($Error[0])`n Ensure that there are NO SelectionChanged properties (PowerShell cannot process them)"
    throw}

#===========================================================================
# Load XAML Objects In PowerShell
#===========================================================================

$xaml.SelectNodes("//*[@Name]") | %{"trying item $($_.Name)";
    try {Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name) -ErrorAction Stop}
    catch{throw}
    }

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

#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

4.運行powershell腳本文件就能夠了
上個圖,這個是參考彪鍋的WinForm版的樣子作的
WPF+PowerShell製做單機版應用函數

5.記得VS studio的這個插件PowerShell Tools Pro,能夠直接在VS studio中進行PowerShell+WPF的開發,原理和上面都是同樣的,只不過是封裝了不少的函數。工具

三.WinForm
WinForm是微軟舊的GUI技術。和PowerShell結合方面與WPF很相似,只不過GUI不是用XAML來寫。最簡單的方法就是用Powershell Studio這個工具來作,看名字就知道了,專門用於PowerShell圖形化工具製做用的。ui

相關文章
相關標籤/搜索