【问题标题】:Bind variables to combobox and textbox in Powershell将变量绑定到 Powershell 中的组合框和文本框
【发布时间】:2021-01-01 06:39:41
【问题描述】:

我会给你带来更多问题。

我已经构建了一个简单的 GUI 来创建打印机或更改打印服务器上的驱动程序。但是,当我单击 RUN 按钮时,它不会做任何事情,只是弹出操作成功的消息。有人可以帮我检查代码并告诉我我做错了什么吗?

#region pop up the gui

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')

Add-Type -AssemblyName PresentationFramework

[xml]$xaml = @"
    <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Print Queue Configuration Tool" Height="800" Width="800" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Topmost="True">
        <Grid>
            <TextBlock HorizontalAlignment="Left" Margin="24,85,0,0" TextWrapping="Wrap" Text="Welcome to Print Queue Configuration Tool" VerticalAlignment="Top" Width="456" FontSize="22" FontWeight="Bold"/>
            <Image HorizontalAlignment="Left" Height="41" Margin="537,36,0,0" VerticalAlignment="Top" Width="257" Source="logo.png"/>
            <TextBlock HorizontalAlignment="Left" Margin="72,140,0,0" TextWrapping="Wrap" Text="What do you want to do?" VerticalAlignment="Top" RenderTransformOrigin="0.422,0.157" Height="31" Width="257" FontSize="16"/>
            <RadioButton x:Name="RadioCreate" Content="Create a print queue." HorizontalAlignment="Left" Margin="102,194,0,0" VerticalAlignment="Top" IsChecked="True"/>
            <RadioButton x:Name="RadioChange" Content="Change driver to a print queue." HorizontalAlignment="Left" Margin="311,194,0,0" VerticalAlignment="Top"/>
            <ComboBox x:Name="ComboMaker" HorizontalAlignment="Left" Margin="460,260,0,0" VerticalAlignment="Top" Width="290">
                <ComboBoxItem Content="Dascom"/>
                <ComboBoxItem Content="HP"/>
                <ComboBoxItem Content="Kyocera"/>
                <ComboBoxItem Content="Lexmark"/>
                <ComboBoxItem Content="Zebra"/>
            </ComboBox>
            <ComboBox x:Name="ComboModel" HorizontalAlignment="Left" Margin="460,310,0,0" VerticalAlignment="Top" Width="290">
            </ComboBox>
            <ComboBox x:Name="ComboTray" HorizontalAlignment="Left" Margin="460,360,0,0" VerticalAlignment="Top" Width="290">
            </ComboBox>
            <ComboBox x:Name="ComboDriver" HorizontalAlignment="Left" Margin="460,410,0,0" VerticalAlignment="Top" Width="290">
            </ComboBox>
            <TextBox x:Name="TextBoxPQ" HorizontalAlignment="Left" Height="22" Margin="460,460,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="290" SelectionOpacity="4"/>
            <TextBox x:Name="TextBoxServer" HorizontalAlignment="Left" Height="22" Margin="460,510,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="290"/>
            <TextBox x:Name="TextBoxHostname" HorizontalAlignment="Left" Height="22" Margin="460,560,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="290"/>
            <TextBox x:Name="TextBoxLocation" HorizontalAlignment="Left" Height="22" Margin="460,610,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="290"/>
            <TextBlock HorizontalAlignment="Left" Margin="72,260,0,0" TextWrapping="Wrap" Text="Select the maker:" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
            <TextBlock HorizontalAlignment="Left" Margin="72,310,0,0" TextWrapping="Wrap" Text="Select the model:" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
            <TextBlock HorizontalAlignment="Left" Margin="72,360,0,0" TextWrapping="Wrap" Text="Select the no. of additional trays (only for model CX825, CX921, CX922, MX910, X950):" VerticalAlignment="Top" Width="304" Height="32" FontWeight="Bold"/>
            <TextBlock HorizontalAlignment="Left" Margin="72,410,0,0" TextWrapping="Wrap" Text="Select the driver:" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
            <TextBlock HorizontalAlignment="Left" Margin="72,460,0,0" TextWrapping="Wrap" Text="Add the print queue name:" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
            <TextBlock HorizontalAlignment="Left" Margin="72,510,0,0" TextWrapping="Wrap" Text="Add the server name:" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
            <TextBlock HorizontalAlignment="Left" Margin="72,560,0,0" TextWrapping="Wrap" Text="Add the hostname:" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
            <TextBlock HorizontalAlignment="Left" Margin="72,610,0,0" TextWrapping="Wrap" Text="Add the location (i.e. DE-Eltmann):" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
            <Button x:Name="ButtonRun" Content="Run" HorizontalAlignment="Left" Margin="660,710,0,0" VerticalAlignment="Top" Width="90" Height="35"/>

        </Grid>
    </Window>
"@  
    
    #Read XAML 
$reader=(New-Object System.Xml.XmlNodeReader $xaml) 
$window = [Windows.Markup.XamlReader]::Load($reader)

    #Add actions to button Run
$RadioCreate = $window.FindName("RadioCreate")


$RadioChange = $window.FindName("RadioChange")


$ComboMaker = $window.FindName("ComboMaker")
$ComboModel = $window.FindName("ComboModel")
$ComboDriver = $window.FindName("ComboDriver")
$ComboMaker.Add_DropdownClosed({
    $ComboModel.Items.Clear()
    $ComboDriver.Items.Clear()

    switch ($ComboMaker.Text) {
        "Dascom" {
            $ComboModel.Items.Add("Dascom T2150s")
            $ComboModel.Items.Add("Dascom T2380")
        }
        "HP" {
            $ComboModel.Items.Add("HP LaserJet M507dn")
            $ComboModel.Items.Add("HP LaserJet M700 M712dn")
            $ComboModel.Items.Add("HP Color LaserJet M553dn")
            $ComboModel.Items.Add("HP OfficeJet Pro 8210")
            $ComboModel.Items.Add("HP PageWide Pro 452dw")
            $ComboModel.Items.Add("HP PageWide Enterprise Color 556dn")
            $ComboModel.Items.Add("HP DesignJet T1600")
        }
        "Kyocera" {
            $ComboModel.Items.Add("Kyocera P3155dn")
        }
        "Lexmark" {
            $ComboModel.Items.Add("Lexmark CX625adhe")
            $ComboModel.Items.Add("Lexmark CX825dte")
            $ComboModel.Items.Add("Lexmark CX921de")
            $ComboModel.Items.Add("Lexmark CX922de")
            $ComboModel.Items.Add("Lexmark MX722adhe")
            $ComboModel.Items.Add("Lexmark MS725dvn")
            $ComboModel.Items.Add("Lexmark MX910de")
            $ComboModel.Items.Add("Lexmark X950de")
        }
        "Zebra" {
            $ComboModel.Items.Add("Zebra ZD620t 300 dpi")
            $ComboModel.Items.Add("Zebra ZT620 300 dpi")
            $ComboModel.Items.Add("Zebra 220Xi4 300 dpi")
            $ComboModel.Items.Add("Zebra ZT610-300 dpi")
            $ComboModel.Items.Add("Zebra ZQ630 Mobile Printer")
        }
    }
    switch ($ComboMaker.Text) {
        "Dascom" {
            $ComboDriver.Items.Add("Tally T2150_")
        }
        "HP" {
            $ComboDriver.Items.Add("HP DesignJet HPGL2 Driver")
            $ComboDriver.Items.Add("HP Designjet T790ps 44in PS3")
            $ComboDriver.Items.Add("HP DesignJet T930 HPGL2")
            $ComboDriver.Items.Add("HP DesignJet Universal Print Driver HPGL2")
            $ComboDriver.Items.Add("HP DesignJet Universal Print Driver HPGL2(v4.5.0)")
            $ComboDriver.Items.Add("HP Universal Printing PS")
            $ComboDriver.Items.Add("HP Universal Printing PS (v6.3.0)")
            $ComboDriver.Items.Add("HP Universal Printing PS (v6.4.1)")
        }
        "Kyocera" {
            $ComboDriver.Items.Add("Kyocera Classic Universaldriver KPDL")
            $ComboDriver.Items.Add("Kyocera Classic Universaldriver KPDL (A4 models)")
        }
        "Lexmark" {
            $ComboDriver.Items.Add("Lexmark Universal v2 PS3")
            $ComboDriver.Items.Add("Lexmark Universal v2 PS3 (2.10.0.0)")
            $ComboDriver.Items.Add("Lexmark Universal v2 PS3 (2.11.0.0)")
            $ComboDriver.Items.Add("Lexmark Universal v2 PS3 (2.13.0.0)")
            $ComboDriver.Items.Add("Lexmark Universal v2 PS3 (2.14.0.0)")
            $ComboDriver.Items.Add("Lexmark Universal v2 PS3 (2.14.1.0)")
        }
        "Zebra" {
            $ComboDriver.Items.Add("ZDesigner 110Xi4 203 dpi")
            $ComboDriver.Items.Add("ZDesigner ZDesigner 110Xi4 300 dpi")
            $ComboDriver.Items.Add("ZDesigner 220Xi4 300 dpi")
            $ComboDriver.Items.Add("ZDesigner GX430t")
            $ComboDriver.Items.Add("ZDesigner QLn420 (CPCL)")

            $ComboDriver.Items.Add("ZDesigner QLn420 (ZPL)")
            $ComboDriver.Items.Add("ZDesigner S4M-203dpi ZPL")
            $ComboDriver.Items.Add("ZDesigner ZD620-300dpi ZPL")
            $ComboDriver.Items.Add("ZDesigner ZM400 200 dpi (EPL)")
            $ComboDriver.Items.Add("ZDesigner ZM400 200 dpi (ZPL)")

            $ComboDriver.Items.Add("ZDesigner ZM400 300 dpi (ZPL)")
            $ComboDriver.Items.Add("ZDesigner ZM600 200 dpi (ZPL)")
            $ComboDriver.Items.Add("ZDesigner ZQ510 (ZPL)")
            $ComboDriver.Items.Add("ZDesigner ZQ630 (ZPL)")
            $ComboDriver.Items.Add("ZDesigner ZT230-200dpi ZPL")

            $ComboDriver.Items.Add("ZDesigner ZT410-300dpi ZPL")
            $ComboDriver.Items.Add("ZDesigner ZT420-300dpi ZPL")
            $ComboDriver.Items.Add("ZDesigner ZT610-300dpi ZPL")
            $ComboDriver.Items.Add("ZDesigner ZT610R-203dpi ZPL")
            $ComboDriver.Items.Add("ZDesigner ZT610R-300dpi ZPL")
        }
    }
    switch ($ComboModel.Text) {
        "Lexmark CX825dte" {
            $ComboTray.Items.Add("3")
        }
        "Lexmark CX921de" {
            $ComboTray.Items.Add("0")
            $ComboTray.Items.Add("1")
            $ComboTray.Items.Add("2")
            $ComboTray.Items.Add("3")
        }
        "Lexmark CX922de" {
            $ComboTray.Items.Add("0")
            $ComboTray.Items.Add("1")
            $ComboTray.Items.Add("2")
            $ComboTray.Items.Add("3")
        }
        "Lexmark MX910de" {
            $ComboTray.Items.Add("0")
            $ComboTray.Items.Add("1")
            $ComboTray.Items.Add("2")
            $ComboTray.Items.Add("3")
        }
        "Lexmark X950de" {
            $ComboTray.Items.Add("0")
            $ComboTray.Items.Add("1")
            $ComboTray.Items.Add("2")
            $ComboTray.Items.Add("3")
        }
    }
})
<# this part contains the variable assignment but not working
$maker = $ComboMaker.SelectedItem
$modelfull = $ComboModel.SelectedItem
$tray = $ComboTray.SelectedItem

$driver = $ComboDriver.SelectedItem

$TextBoxPQ = $window.FindName("TextBoxPQ")
$PQ = $TextBoxPQ.Text

$TextBoxServer = $window.FindName("TextBoxServer")
$server = $TextBoxServer.Text

$TextBoxHostname = $window.FindName("TextBoxHostname")
$hostname = $TextBoxHostname.Text

$TextBoxLocation = $window.FindName("TextBoxLocation")
$location = $TextBoxLocation.Text

#>

$ButtonRun = $window.FindName("ButtonRun")
$ButtonRun.Add_Click({
    # here variables are not working as well
    $maker = $ComboMaker.SelectedItem
    $modelfull = $ComboModel.SelectedItem
    $tray = $ComboTray.SelectedItem

    $driver = $ComboDriver.SelectedItem

    $TextBoxPQ = $window.FindName("TextBoxPQ")
    $PQ = $TextBoxPQ.Text

    $TextBoxServer = $window.FindName("TextBoxServer")
    $server = $TextBoxServer.Text

    $TextBoxHostname = $window.FindName("TextBoxHostname")
    $hostname = $TextBoxHostname.Text

    $TextBoxLocation = $window.FindName("TextBoxLocation")
    $location = $TextBoxLocation.Text

    if ($RadioCreate.Checked -eq $true){
        PortCreation
        PrinterCreation
        if ($maker -eq 'Lexmark') {
            ApplyConfigLexmark
        }
    }
    if ($RadioChange.Checked -eq $true) {
        ChangeDriver
        if ($maker -eq 'Lexmark') {
            ApplyConfigLexmark
        }
    }
    [System.Windows.MessageBox]::Show('Operation completed!')

})
    
    #Show form

$window.ShowDialog() | Out-Null

#endregion

我还想将模型选择链接到另一个名为“模型”的变量,该变量将使用模型的快捷方式来部署一些特定设置,一旦弹出“操作成功消息”并按下“确定”,所有组合框和 texbox重置为空白。

仅供参考:PortCreation、PrinterCreation、ChangeDriver 和 ApplyConfigLexmark 是在 Powershell 中执行代码的函数,所以我不会放整个代码。我使用的变量如下:

$maker # choose the brand of the physical device
$model # short name of the device model
$modelfull # Full device model
$tray # number of the trays the device has
$driver # Driver name
$PQ # print queue name
$server # hostname of the server
$hostname # hostname of the printer
$location # location of the device

【问题讨论】:

  • 检查我的更新答案,如果有帮助或有任何问题,请告诉我

标签: wpf powershell xaml user-interface


【解决方案1】:

我测试了您的代码并更改了这一行以显示制造商并且它可以工作。为什么说变量没有绑定?

[System.Windows.MessageBox]::Show('Operation completed!' + $maker)

Also Checked 应该是 IsChecked

if ($RadioCreate.IsChecked -eq $true){

【讨论】:

  • 嗨@Hassan,当我按下运行按钮时,程序什么也没做,甚至没有启动创建打印队列的过程。我已经挣扎了很长时间,老实说,我不知道我还能做些什么来让它发挥作用
  • 更新。它正在工作,但只有创作部分:) 我会弄清楚为什么另一部分不工作。现在我需要添加更多 MessageBox 以使其与用户更具交互性。非常感谢!
  • @Menistro 太好了,我的回答有帮助吗?
猜你喜欢
  • 1970-01-01
  • 2016-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-18
  • 1970-01-01
相关资源
最近更新 更多