WPF+PowerShell制作單機版應(yīng)用-創(chuàng)新互聯(lián)

我接觸到的PowerShell GUI化工具制作有三種方式
一.Web-------------B/S架構(gòu)
二.WPF------------C/S架構(gòu)
三.WinForm-------C/S架構(gòu)

創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的興山網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

一.Web
詳見我的另一篇 https://blog.51cto.com/640006/2059918

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

2.把XAML代碼直接粘貼到下面PowerShell代碼的@""@里,或者XAML另存為一個文件,然后在PowerShell代碼里用Get-Content加載進來也可以。

3.下面是用PowerShell操作XAML的代碼,foxdeploy已經(jīng)寫好的模板,直接拿來用就行。這里面沒寫事件控制,具體的看foxdeploy博客吧,和WinForm的一樣。

#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("http://*[@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制作單機版應(yīng)用

5.記得VS studio的這個插件PowerShell Tools Pro,可以直接在VS studio中進行PowerShell+WPF的開發(fā),原理和上面都是一樣的,只不過是封裝了很多的函數(shù)。

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

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)頁標題:WPF+PowerShell制作單機版應(yīng)用-創(chuàng)新互聯(lián)
文章起源:http://muchs.cn/article46/ddjheg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、商城網(wǎng)站營銷型網(wǎng)站建設(shè)、用戶體驗、關(guān)鍵詞優(yōu)化虛擬主機

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營