【问题标题】:Powershell/Xaml GUI - Variables, Controls, Dynamic Scripts, etcPowershell/Xaml GUI - 变量、控件、动态脚本等
【发布时间】:2019-01-07 17:32:44
【问题描述】:

我偶然发现了将 xaml、powershell 和 cscript 文件集成到“可以在 Windows 上执行任何操作”类型的程序中的可爱概念。尽管 15 年前我在大学里做过一点 .net,但我从未真正接触过 c#...

我最近的发现让我想了解更多关于它的信息,然而,当我开始使用与过去相同的方法来设计一些 GUI 时,用于编码 HTML 表格,最终使用 CSS Flex/Grid (几乎相同的想法)...我不禁想知道是否有一种方法可以更快更有效地做到这一点。

在学习如何集成 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 文件的情况下完成,那就太糟糕了。

【问题讨论】:

    标签: c# .net powershell xaml user-interface


    【解决方案1】:

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

    示例控制台应用程序: Friday Fun – A PowerShell Console Menu 作者的例子:

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

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

    Creating Menus in Powershell

    否则在整个网络上都有大量的 WinForms 和 WPF GUI 开发示例,

    示例: PowerShell and WPF: Introduction and Building Your First Window

    如果您更喜欢视觉学习者,请观看 YouTube 视频:

    powershell wpf gui

    关于问题的 OP 更新

    明白了。 至于……

    “基本上,我想在 Xaml 中创建变量,以便我知道 powershell.... $gridrow = 3 ...",

    因此,您所追求的更多细节/示例至关重要。然而,您可以在 XAML 中定义变量,这在 MS 文档和问答帖子中有记录。

    How can I define a variable in XAML?

    Can I declare variable in the XAML code?

    根据文章

    # 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();   
            }   
        }  
    

    【讨论】:

    • 我更新了顶部以更准确地反映我所追求的原则。我不是在尝试制作 powershell 控制台或批处理脚本。我正在尝试完全消除 xaml 文件或 xaml 文件中的大量重复代码。
    • 这看起来是一个不错的想法或方法,可能会导致我正在寻找实现。我尝试完全远离 C# 编程,但我开始发现这可能是不可能的?我还不太懂 C#,也许我只是需要一些关于 Powershell 有何不同的入门知识?
    • 是的,从长远来看,PowerShell 是通向 C# 的入门药物。 PowerShell 和 C# 是两个不同的东西,因此,尝试进行苹果对苹果的比较并不是真正的旅行。最好知道直接在 PowerShell 中可以做什么,以及何时需要添加 C# 的东西,反之亦然。您可以使用 - Add-Type: docs.microsoft.com/en-us/powershell/module/… 将 C# 代码添加到 PowerShell 脚本
    • 哇,这非常有用。 c# 和 c++ 是完全不同的故事,我需要更多时间才能理解。当涉及到批处理和 powershell 时,我可以完成很多工作,因为我了解动态变量的概念......当涉及到 c# 时,我往往会崩溃。与 javascript 相同...但是。也许一旦我完成了我正在处理的这个 GUI,我最终可以做我想做的事情。我一直在寻找一种方法来缩短我正在尝试做的工作的整体结构,但我认为复制+粘贴现在就可以了,哈哈
    猜你喜欢
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    相关资源
    最近更新 更多