Wpf Powershell GUI-选择单选按钮后将变量添加到单选按钮

Wpf Powershell GUI-选择单选按钮后将变量添加到单选按钮,wpf,visual-studio,powershell,Wpf,Visual Studio,Powershell,我正在为我为Powershell制作的脚本在Visual Studuio中执行GUI,我需要使用单选按钮添加变量以使该脚本工作,例如,该脚本与5种打印机品牌一起工作,因此用户需要选择其中一种进行操作。然后,对于模型中的每个品牌。最后,将执行代码在所选服务器上创建打印队列,其中包括以下单选按钮上保留的变量: $maker - brand of the device. $model - model of the device. $modelfull - full model name for com

我正在为我为Powershell制作的脚本在Visual Studuio中执行GUI,我需要使用单选按钮添加变量以使该脚本工作,例如,该脚本与5种打印机品牌一起工作,因此用户需要选择其中一种进行操作。然后,对于模型中的每个品牌。最后,将执行代码在所选服务器上创建打印队列,其中包括以下单选按钮上保留的变量:

$maker - brand of the device.
$model - model of the device.
$modelfull - full model name for comment purposes.
$driver - print driver to assign to the print queue.
如何将这些变量添加到每个单选按钮?也就是说,我有两个模型,Deskjet 3050和Deskjet 3045,一旦用户选择单选按钮并按下窗口上的“下一步”,如何将这些模型转换为变量

下面您可以看到Visual Studio中的代码示例,其中是带有单选按钮的框架:

<Page x:Class="Print_Queue_Configuration_Tool.Maker_2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:local="clr-namespace:Print_Queue_Configuration_Tool"
mc:Ignorable="d" 
d:DesignHeight="570" d:DesignWidth="754"
Title="Maker_2">
    <Grid x:Name="Maker_2_Grid">
        <Button x:Name="Maker_2_ButtonBack" Content="Back" HorizontalAlignment="Left" Margin="360,525,0,0" VerticalAlignment="Top" Width="120" Height="35"/>
        <Button x:Name="Maker_2_ButtonNext" Content="Next" HorizontalAlignment="Left" Margin="485,525,0,0" VerticalAlignment="Top" Width="120" Height="35"/>
        <Button x:Name="Maker_2_ButtonCancel" Content="Cancel" HorizontalAlignment="Left" Margin="610,525,0,0" VerticalAlignment="Top" Width="120" Height="35" IsCancel="True"/>
        <Image x:Name="Maker_2_ImageLogo" HorizontalAlignment="Left" Height="44.264" Margin="480.157,30.312,0,0" VerticalAlignment="Top" Width="273.843" Source="logo.png" RenderTransformOrigin="0.5,0.5"/>
        <TextBlock x:Name="Maker_2_TextMaker_2" HorizontalAlignment="Left" Margin="70,140,0,0" TextWrapping="Wrap" Text="Please select the maker of the device:" VerticalAlignment="Top"/>
        <RadioButton x:Name="Maker_2_RadioMaker1" Content="Canon" VerticalAlignment="Center" Margin="272.25,220.25,351.75,333.75" Height="16" Width="130" GroupName="Makers_2" FontWeight="Bold"/>
        <RadioButton x:Name="Maker_2_RadioMaker2" Content="Epson" VerticalAlignment="Center" Margin="272.25,260.25,351.75,293.75" Height="16" Width="130" GroupName="Makers_2" FontWeight="Bold"/>
        </Grid>
</Page>
谢谢


PS:我在这方面是新手,所以我还在学习。请保持礼貌,记住你曾经处于我的位置:

这里你的页面正在运行。。。我不得不稍微调整一下xml

请注意FindName以寻址单选按钮


现在它按要求工作。在Function Check Radios(功能检查收音机)中,您可以根据选择Radiobutton(收音机按钮)设置变量。此示例满足您的要求,即当您按下Next(下一步)时,将显示正确的单选按钮选择。这会让你走得更远

[xml]$xaml = @"
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="Window"
    Title="Maker_2" Height="770" Width="800">
    <Grid x:Name="Maker_2_Grid">
        <Button x:Name="Maker_2_ButtonBack" Content="Back" HorizontalAlignment="Left" Margin="360,525,0,0" VerticalAlignment="Top" Width="120" Height="35"/>
        <Button x:Name="Maker_2_ButtonNext" Content="Next" HorizontalAlignment="Left" Margin="485,525,0,0" VerticalAlignment="Top" Width="120" Height="35"/>
        <Button x:Name="Maker_2_ButtonCancel" Content="Cancel" HorizontalAlignment="Left" Margin="610,525,0,0" VerticalAlignment="Top" Width="120" Height="35" IsCancel="True"/>
        <Image x:Name="Maker_2_ImageLogo" HorizontalAlignment="Left" Height="44.264" Margin="480.157,30.312,0,0" VerticalAlignment="Top" Width="273.843" Source="logo.png" RenderTransformOrigin="0.5,0.5"/>
        <TextBlock x:Name="Maker_2_TextMaker_2" HorizontalAlignment="Left" Margin="70,140,0,0" TextWrapping="Wrap" Text="Please select the maker of the device:" VerticalAlignment="Top"/>
        <RadioButton x:Name="Maker_2_RadioMaker1" Content="Canon" VerticalAlignment="Center" Margin="272.25,220.25,351.75,333.75" Height="16" Width="130" GroupName="Makers_2" FontWeight="Bold"/>
        <RadioButton x:Name="Maker_2_RadioMaker2" Content="Epson" VerticalAlignment="Center" Margin="272.25,260.25,351.75,293.75" Height="16" Width="130" GroupName="Makers_2" FontWeight="Bold"/>
        </Grid>
</Window>
"@

function Check-Radios
{   
   $Global:radioSelected = $this;
}

$radioSelected = $null

$reader = (New-Object System.Xml.XmlNodeReader $xaml)


$window = [Windows.Markup.XamlReader]::Load($reader)

$window.FindName("Maker_2_RadioMaker1").add_Checked({Check-Radios})
$window.FindName("Maker_2_RadioMaker2").add_Checked({Check-Radios})
$window.FindName("Maker_2_ButtonNext").add_Click({Write-Host "You have selected: $($radioSelected.Content)"});


$window.ShowDialog()
你可以简单地重写你的下一步按钮点击方法如下

$window.FindName("Maker_2_ButtonNext").add_Click({ Write-Host $($printerList | where Maker -eq $($radioSelected.Content)) });
它将为您提供所选制造商的打印机

使用Navigationwindow和pages,您现在可以转到所选打印机中的页面


致以最诚挚的问候

不幸的是,将wpf作为powershell的xaml导入对我来说并不完全起作用

我为您设计了一个示例,其中所有元素都是在代码中创建的,并且还意识到您首先选择制造商单选按钮,然后在下一页上列出模型或完整模型,两者都作为示例列出。但实际上,您只需要完整模型的组合

这就是Powershell

using namespace System.Windows
using namespace System.Windows.Controls
using namespace System.Windows.Navigation


[System.Reflection.Assembly]::LoadWithPartialName("PresentationFramework")
[System.Reflection.Assembly]::LoadWithPartialName("PresentationCore")
[System.Reflection.Assembly]::LoadWithPartialName("WindowsBase")

function Check-Radios
{   
    $Global:selectedMaker = $this.Content
}

$printerList = [System.Collections.ArrayList]::new()
$printerList.Add([pscustomobject]@{Maker = "Canon";Model = "Pixma";ModelFull="Pixma0815";driver = "driverxyz1"})
$printerList.Add([pscustomobject]@{Maker = "Canon";Model = "Pixma";ModelFull="Pixma1615";driver = "driverxyz1"})
$printerList.Add([pscustomobject]@{Maker = "Epson";Model = "Workforce";ModelFull="Workforce2020";driver = "driverxyz2"})

$selectedMaker = "Canon";

$navigationWindow = [NavigationWindow]::new()
$navigationPage1 = [Page]::new()
$navigationPage2 = [Page]::new()
#Page with printers
$printerLabel = [Label]::new()
$printerLabel.Content = ($printerList | where Maker -eq $selectedMaker | Select -First 1).Maker
$printerModelCombo = [ComboBox]::new()
$printerModelFullCombo = [ComboBox]::new()


$printerStackPanel = [StackPanel]::new()
$printerStackPanel.AddChild($printerLabel)
$printerStackPanel.AddChild($printerModelCombo)
$printerStackPanel.AddChild($printerModelFullCombo)
$navigationPage2.Content = $printerStackPanel


#Page with radiobuttons
$radioButton1 = [RadioButton]::new()
$radioButton1.Content ="Canon"
$radioButton1.Name = "RadioCanon"
$radioButton2 = [RadioButton]::new()
$radioButton2.Content ="Epson"
$radioButton2.Name = "RadioEpson"

$radioButton1.add_Checked({Check-Radios})
$radioButton2.add_Checked({Check-Radios})
$button1 = [Button]::new()
$button1.Content = "Next"
$button1.add_Click(
{
    foreach($entry in ($printerList | where Maker -eq $selectedMaker))
    {
        # Model
        if($printerModelCombo.Items.IsEmpty -or !$printerModelCombo.Items.Content.Contains($entry.Model))
        {
            $printerModelComboItem = [ComboBoxItem]::new()
            $printerModelComboItem.Content = $entry.Model
            $printerModelCombo.AddChild($printerModelComboItem)    
        }
        # Model Full
        $printerModelFullComboItem = [ComboBoxItem]::new()
        $printerModelFullComboItem.Content = $entry.ModelFull
        $printerModelFullCombo.AddChild($printerModelFullComboItem)    
        
    }
   

    $printerModelCombo.SelectedIndex = 0
    $printerModelFullCombo.SelectedIndex = 0

    
    $navigationWindow.NavigationService.Content = $navigationPage2
})


$grid = [Grid]::new()
$gridRowDef1 = [RowDefinition]::new()
$gridRowDef2 = [RowDefinition]::new()
$gridRowDef1.Height ="4*"
$gridRowDef2.Height ="1*"


$grid.RowDefinitions.Add($gridRowDef1)
$grid.RowDefinitions.Add($gridRowDef2)


$stackPanel = [StackPanel]::new()

$stackPanel.AddChild($radioButton1)
$stackPanel.AddChild($radioButton2)
$grid.AddChild($stackPanel)
$grid.AddChild($button1)
[Grid]::SetRow($stackPanel,0)
[Grid]::SetRow($button1,1)



$navigationPage1.Content = $grid;
$navigationWindow.Title = "Navigation"
$navigationWindow.Width = 960
$navigationWindow.Height = 600
$navigationWindow.NavigationService.Content = $navigationPage1
$navigationWindow.ShowsNavigationUI = $false
$navigationWindow.ShowDialog()


到底是什么不起作用?您的变量$maker将不设置吗?当您输出$wpf.Maker\u 2\u RadioMaker1时,变量中是否有什么内容?如何启动对话框?必须使用findname来处理按钮。你的页面必须放在导航窗口中。Hello@RoXTar,我终于设法让GUI工作了,但我没有使用单选按钮,而是使用普通按钮。我使用了您示例中的一些想法,将其向前推进到初始GUI,但部分工作正常,可能是因为我在这方面的培训不够。非常感谢你在这个话题上所做的努力
$printerList = [System.Collections.ArrayList]::new()
$printerList.Add([pscustomobject]@{Maker = "Canon";Model = "Pixma";ModelFull="Pixma0815";driver = "driverxyz1"})
$printerList.Add([pscustomobject]@{Maker = "Epson";Model = "Workforce";ModelFull="Workforce2020";driver = "driverxyz2"})
$window.FindName("Maker_2_ButtonNext").add_Click({ Write-Host $($printerList | where Maker -eq $($radioSelected.Content)) });
using namespace System.Windows
using namespace System.Windows.Controls
using namespace System.Windows.Navigation


[System.Reflection.Assembly]::LoadWithPartialName("PresentationFramework")
[System.Reflection.Assembly]::LoadWithPartialName("PresentationCore")
[System.Reflection.Assembly]::LoadWithPartialName("WindowsBase")

function Check-Radios
{   
    $Global:selectedMaker = $this.Content
}

$printerList = [System.Collections.ArrayList]::new()
$printerList.Add([pscustomobject]@{Maker = "Canon";Model = "Pixma";ModelFull="Pixma0815";driver = "driverxyz1"})
$printerList.Add([pscustomobject]@{Maker = "Canon";Model = "Pixma";ModelFull="Pixma1615";driver = "driverxyz1"})
$printerList.Add([pscustomobject]@{Maker = "Epson";Model = "Workforce";ModelFull="Workforce2020";driver = "driverxyz2"})

$selectedMaker = "Canon";

$navigationWindow = [NavigationWindow]::new()
$navigationPage1 = [Page]::new()
$navigationPage2 = [Page]::new()
#Page with printers
$printerLabel = [Label]::new()
$printerLabel.Content = ($printerList | where Maker -eq $selectedMaker | Select -First 1).Maker
$printerModelCombo = [ComboBox]::new()
$printerModelFullCombo = [ComboBox]::new()


$printerStackPanel = [StackPanel]::new()
$printerStackPanel.AddChild($printerLabel)
$printerStackPanel.AddChild($printerModelCombo)
$printerStackPanel.AddChild($printerModelFullCombo)
$navigationPage2.Content = $printerStackPanel


#Page with radiobuttons
$radioButton1 = [RadioButton]::new()
$radioButton1.Content ="Canon"
$radioButton1.Name = "RadioCanon"
$radioButton2 = [RadioButton]::new()
$radioButton2.Content ="Epson"
$radioButton2.Name = "RadioEpson"

$radioButton1.add_Checked({Check-Radios})
$radioButton2.add_Checked({Check-Radios})
$button1 = [Button]::new()
$button1.Content = "Next"
$button1.add_Click(
{
    foreach($entry in ($printerList | where Maker -eq $selectedMaker))
    {
        # Model
        if($printerModelCombo.Items.IsEmpty -or !$printerModelCombo.Items.Content.Contains($entry.Model))
        {
            $printerModelComboItem = [ComboBoxItem]::new()
            $printerModelComboItem.Content = $entry.Model
            $printerModelCombo.AddChild($printerModelComboItem)    
        }
        # Model Full
        $printerModelFullComboItem = [ComboBoxItem]::new()
        $printerModelFullComboItem.Content = $entry.ModelFull
        $printerModelFullCombo.AddChild($printerModelFullComboItem)    
        
    }
   

    $printerModelCombo.SelectedIndex = 0
    $printerModelFullCombo.SelectedIndex = 0

    
    $navigationWindow.NavigationService.Content = $navigationPage2
})


$grid = [Grid]::new()
$gridRowDef1 = [RowDefinition]::new()
$gridRowDef2 = [RowDefinition]::new()
$gridRowDef1.Height ="4*"
$gridRowDef2.Height ="1*"


$grid.RowDefinitions.Add($gridRowDef1)
$grid.RowDefinitions.Add($gridRowDef2)


$stackPanel = [StackPanel]::new()

$stackPanel.AddChild($radioButton1)
$stackPanel.AddChild($radioButton2)
$grid.AddChild($stackPanel)
$grid.AddChild($button1)
[Grid]::SetRow($stackPanel,0)
[Grid]::SetRow($button1,1)



$navigationPage1.Content = $grid;
$navigationWindow.Title = "Navigation"
$navigationWindow.Width = 960
$navigationWindow.Height = 600
$navigationWindow.NavigationService.Content = $navigationPage1
$navigationWindow.ShowsNavigationUI = $false
$navigationWindow.ShowDialog()