温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

WPF+PowerShell制作单机版应用

发布时间:2020-07-04 04:58:23 来源:网络 阅读:3788 作者:tliursc 栏目:系统运维

我接触到的PowerShell GUI化工具制作有三种方式
一.Web-------------B/S架构
二.WPF------------C/S架构
三.WinForm-------C/S架构

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

二.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的一样。

#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图形化工具制作用的。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI