PowerShell GUI中的ComboBox

时间:2018-03-11 02:43:22

标签: powershell xaml

我一直在使用XAML在powershell中构建GUI一段时间。一切都很好,直到我试图引入ComboBox。然后powershell不再接受它。

我在Visual Studio中构建了XAML,它在“设计”视图中工作。我去的每个网站都告诉我这是正确的XAML代码。代码发生错误 $reader=(New-Object System.Xml.XmlNodeReader $xaml) try{$DiagForm=[Windows.Markup.XamlReader]::Load( $reader )}

以下是我正在使用的示例。如果你拿出ComboBox片段,它的效果非常好。

function Diag {
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = @'
    <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Tool" Height="500" Width="300" WindowStartupLocation="CenterScreen" WindowStyle='SingleBorderWindow' ResizeMode='CanMinimize'>
    <Grid>
        <TextBox HorizontalAlignment="Center" Height="23" TextWrapping="Wrap" Text="Diagnostics" VerticalAlignment="Top" Width="300" Margin="0,-1,-0.2,0" TextAlignment="Center" Foreground="White" Background="#21C500"/>
        <Label Content="Report" HorizontalContentAlignment="Center" HorizontalAlignment="Left" Margin="90,27,0,0" VerticalAlignment="Top" Width="120"/>
        <ComboBox HorizontalAlignment="Left" Margin="90,60,0,0" VerticalAlignment="Top" Width="120">
            <ComboBoxItem MouseMove="OnHover" Name="Period1" IsSelected="True">Last 24hr</ComboBoxItem>
            <ComboBoxItem MouseMove="OnHover" Name="Period2" IsSelected="False">Last Week</ComboBoxItem>
            <ComboBoxItem MouseMove="OnHover" Name="Period3" IsSelected="False">Last Month</ComboBoxItem>
        </ComboBox>
        <Label Content="Critical" HorizontalAlignment="Left" Margin="46,115,0,0" VerticalAlignment="Top"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="124,115,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
        <Label Content="Warnings" HorizontalAlignment="Left" Margin="46,143,0,0" VerticalAlignment="Top"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="124,143,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    </Grid>
    </Window>
'@
    #Read XAML
    $reader=(New-Object System.Xml.XmlNodeReader $xaml) 
    try{$DiagForm=[Windows.Markup.XamlReader]::Load( $reader )}
    catch{Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."; exit}

    $xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $DiagForm.FindName($_.Name)}

$DiagForm.ShowDialog() | out-null
}
Diag

1 个答案:

答案 0 :(得分:0)

如果检查正在捕获的异常,您将看到以下错误消息:

  

无法创建“鼠标移动”&#39;来自文本&#39; OnHover&#39;。

的确,如果从XAML中删除MouseMove="OnHover"属性,问题就会消失。