Powershell不显示XAML对话框

时间:2018-03-15 17:37:54

标签: .net powershell xaml

我在Powershell Windows 10中复制并粘贴了以下源代码: https://mcpmag.com/articles/2016/04/28/building-ui-using-powershell.aspx

控制台没有错误但没有显示对话框?

    [xml]$XAML  = @"

      <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

      xmlns:local="clr-namespace:MyFirstWPF"

      Title="PowerShell Computer Utility" Height="350"  Width="525">

      <Grid>

      <GroupBox  x:Name="Actions" Header="Actions"  HorizontalAlignment="Left" Height="299"  VerticalAlignment="Top" Width="77" Margin="0,11,0,0">

      <StackPanel>

      <Button  x:Name="Services_btn" Content="Services"/>

      <Label  />

      <Button  x:Name="Processes_btn" Content="Processes"/>

      <Label  />

      <Button  x:Name="Drives_btn" Content="Drives"/>

      </StackPanel>

      </GroupBox>

      <GroupBox  x:Name="Computername" Header="Computername"  HorizontalAlignment="Left" Margin="92,11,0,0"  VerticalAlignment="Top" Height="45" Width="415">

      <TextBox  x:Name="InputBox_txtbx" TextWrapping="Wrap"/>

      </GroupBox>

      <GroupBox  x:Name="Results" Header="Results"  HorizontalAlignment="Left" Margin="92,61,0,0"  VerticalAlignment="Top" Height="248" Width="415">

      <TextBox  x:Name="Output_txtbx" IsReadOnly="True"  HorizontalScrollBarVisibility="Auto"  VerticalScrollBarVisibility="Auto" />

    </GroupBox>
        </Grid>

      </Window>

      "@ 


$reader=(New-Object System.Xml.XmlNodeReader  $xaml)
$Window=[Windows.Markup.XamlReader]::Load(  $reader ) 

    #Connect to Controls 

      $xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]")  | ForEach {

      New-Variable  -Name $_.Name -Value $Window.FindName($_.Name) -Force

      } 

$Null = $Window.ShowDialog() 

1 个答案:

答案 0 :(得分:3)

您的XAML对PowerShell无效。

删除代码属性前面的x:等(例如x:Name => Name)。此外,删除除第一个之外的所有xml命名空间属性。

以下对我有用:

[xml] $XAML = @"
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    Title="PowerShell Computer Utility" Height="350" Width="525">
<Grid>
    <GroupBox Name="Actions" Header="Actions" HorizontalAlignment="Left" Height="299" VerticalAlignment="Top" Width="77" Margin="0,11,0,0">
        <StackPanel>
            <Button Name="Services_btn" Content="Services"/>
            <Label />
            <Button Name="Processes_btn" Content="Processes"/>
            <Label />
            <Button Name="Drives_btn" Content="Drives"/>
        </StackPanel>
    </GroupBox>
    <GroupBox Name="Computername" Header="Computername"  HorizontalAlignment="Left" Margin="92,11,0,0"  VerticalAlignment="Top" Height="45" Width="415">
        <TextBox Name="InputBox_txtbx" TextWrapping="Wrap"/>
    </GroupBox>
    <GroupBox Name="Results" Header="Results"  HorizontalAlignment="Left" Margin="92,61,0,0"  VerticalAlignment="Top" Height="248" Width="415">
        <TextBox Name="Output_txtbx" IsReadOnly="True"  HorizontalScrollBarVisibility="Auto"  VerticalScrollBarVisibility="Auto" />
    </GroupBox>
</Grid>
</Window>
"@ 

Add-Type -AssemblyName PresentationFramework
$reader = [System.Xml.XmlNodeReader]::new($xaml)
$Window = [Windows.Markup.XamlReader]::Load($reader) 

# Connect to Controls 
$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") |
    ForEach-Object {
        New-Variable  -Name $_.Name -Value $Window.FindName($_.Name) -Force
    }

$Null = $Window.ShowDialog()