Powershell/Xaml GUI-变量、控件、动态脚本等



我偶然发现了将xaml、powershell和cscript文件集成到"一个可以在windows上做任何事情的程序"类型中的可爱概念。尽管15年前我在大学里学过一点.net,但我从未真正进入过c。。。

我最近的发现让我想了解更多关于它的信息,然而,当我开始使用与过去相同的方法设计一些GUI时,我曾使用这些方法来编写HTML表,并最终使用(几乎相同的想法)。。。我忍不住想知道是否有办法更快、更有效地做到这一点。

当我学习如何集成PS/XAML时,我很好奇是否可能一开始就需要XAML文件。比如,如果我可以列出一个变量列表,其中包含通过应答文件或模板调用的嵌套变量,而不需要调用Xaml文件,该怎么办?

也许这毫无意义,也许每个场景都需要xaml,这很好。。。但是我可以用ps脚本定义的变量来编码xml吗?而且cscript文件对于完成这一切是必要的吗?

未附加PS/XAML代码。在这一点上只是一个假设。。。但我将提供一个我的批处理文件想法的例子,它说明了我希望在这里实现的点。。。

我不完全确定这是否可行?但减少"做事"所需的时间似乎是个很酷的主意

set "a1=GOOD"
set "h1=ECHO # _______________________________________________________ #"
set "h2=ECHO #                                                         #"
set "h3=ECHO # **********                                   ********** #"
set "h4=ECHO # ******************************************************* #"
set "h5=ECHO #                        %a1%                             #"
set a0="%h4%^%h4%^%h3%^%h3%^%h2%^%h5%^%h2%^%h3%^%h3%^%h4%^%h4%"
set a1=GOOD
%a0%
set a1=BEANS
%a0%

我将很快展示我想要做的事情的示例。

基本上,我想在Xaml中创建变量,这样我就可以告诉powershell。。。。

$gridrow = 3
$gridcol = 4
$currentgrid = $gridrow (3) * $gridcol (4) = (12)
$cg1 = horizontalignment="center", etc content="section 1"
$cg2 = etc etc so forth

然后它会有一个完整的Xaml文件,我可以更容易地复制和编辑。。。

很明显,它可以用其他方法来完成,但如果可以在不保存到xaml文件的情况下完成,那就太糟糕了。

使用Windows窗体或WPF创建PowerShell GUI。您发布的是一个批处理文件。如果你想要的是PowerShell,你仍然可以使用它创建控制台GUI。

控制台应用程序示例:Friday Fun–PowerShell控制台菜单作者示例:

#Requires -version 2.0
<#
-----------------------------------------------------------------------------
Script: Demo-ConsoleMenu.ps1
Version: 1.0
Author: Jeffery Hicks
http://jdhitsolutions.com/blog
http://twitter.com/JeffHicks
http://www.ScriptingGeek.com
Date: 12/30/2011
Keywords: Read-Host, Menu, Switch
Comments:
The best way to create a menu is to use a here string
Use -ClearScreen if you want to run CLS before displaying the menu
"Those who forget to script are doomed to repeat their work."
****************************************************************
* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
* THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK.  IF   *
* YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
* DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.             *
****************************************************************
-----------------------------------------------------------------------------
#>
Function Show-Menu {
Param(
[Parameter(Position=0,Mandatory=$True,HelpMessage="Enter your menu text")]
[ValidateNotNullOrEmpty()]
[string]$Menu,
[Parameter(Position=1)]
[ValidateNotNullOrEmpty()]
[string]$Title="Menu",
[switch]$ClearScreen
)
if ($ClearScreen) {Clear-Host}
#build the menu prompt
$menuPrompt=$title
#add a return
$menuprompt+="`n"
#add an underline
$menuprompt+="-"*$title.Length
$menuprompt+="`n"
#add the menu
$menuPrompt+=$menu
Read-Host -Prompt $menuprompt
} #end function
#define a menu here string
$menu=@"
1 Show info about a computer
2 Show info about someones mailbox
3 Restarts the print spooler
Q Quit
Select a task by number or Q to quit
"@
#Keep looping and running the menu until the user selects Q (or q).
Do {
#use a Switch construct to take action depending on what menu choice
#is selected.
Switch (Show-Menu $menu "My Help Desk Tasks" -clear) {
"1" {Write-Host "run get info code" -ForegroundColor Yellow
sleep -seconds 2
} 
"2" {Write-Host "run show mailbox code" -ForegroundColor Green
sleep -seconds 5
}
"3" {Write-Host "restart spooler" -ForegroundColor Magenta
sleep -seconds 2
}
"Q" {Write-Host "Goodbye" -ForegroundColor Cyan
Return
}
Default {Write-Warning "Invalid Choice. Try again."
sleep -milliseconds 750}
} #switch
} While ($True)

这里的另一个控制台示例:

在Powershell 中创建菜单

除此之外,还有很多WinForms和WPF GUI开发的例子,在整个网络上,

示例:PowerShell和WPF:介绍和构建您的第一个窗口

YouTube视频,如果你是一个视觉学习者:

powershell wpf gui

问题的OP更新

明白了。至于…

"基本上,我想在Xaml中创建变量,这样我就可以判断powershell$gridrow=3。。。",

因此,您所追求的更多细节/示例至关重要。然而,您可以在XAML中定义变量,并在MS文档和Q&A发帖。

如何在XAML中定义变量?

我可以在XAML代码中声明变量吗?

根据文章

# After definitition:
<Window x:Class="WpfApplication1_delete_test.Window1"  
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
xmlns:System="clr-namespace:System;assembly=mscorlib"  
Title="Window1">   
<Window.Resources>  
<System:String x:Key="myStr">init</System:String>  
</Window.Resources>  
<TextBlock Name="txtBlock" Text="{Binding Source={StaticResource myStr}}"/>   
</Window>  
# you can use from code like that:
public partial class Window1 : Window   
{   
public Window1()   
{   
InitializeComponent();   
Resources["myStr"] = "string";   
txtBlock.Text = FindResource("myStr").ToString();   
}   
}  

最新更新