【问题标题】:WPF GUI to powershellWPF GUI 到 powershell
【发布时间】:2018-09-27 14:54:43
【问题描述】:

不知何故,我的 XAML 代码无法在 PowerShell 中运行。

<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:SD_Tools"
    mc:Ignorable="d"
    Title="Service Desk Tools" Height="450" Width="800" WindowStyle="None" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Border Name="Content" BorderBrush="Black" BorderThickness="0" HorizontalAlignment="Left" Height="412" Margin="139,38,0,0" VerticalAlignment="Top" Width="661" Background="#FFECF0F3"/>
    <Border Name="Nav" BorderBrush="Black" BorderThickness="0" HorizontalAlignment="Left" Height="450" VerticalAlignment="Top" Width="139" Background="#FF334351">
        <Border.Effect>
            <DropShadowEffect BlurRadius="16" Direction="361" RenderingBias="Quality" Color="#FFC5BEBE"/>
        </Border.Effect>
    </Border>
    <Border Name="Top" BorderBrush="Black" BorderThickness="0" HorizontalAlignment="Left" Height="38" Margin="139,0,0,0" VerticalAlignment="Top" Width="661" Background="#FF6B7B9E">
        <Border.Effect>
            <DropShadowEffect BlurRadius="16" Direction="1" RenderingBias="Quality" Color="#FFC5BEBE"/>
        </Border.Effect>
        <Button Name="Button_close" Content="X" HorizontalAlignment="Left" Height="38" Margin="615,0,0,0" VerticalAlignment="Top" Width="46" Background="{Null}" Foreground="White" FontFamily="Century Gothic" FontSize="16" FontWeight="Light" BorderBrush="{Null}"/>
    </Border>
    <Button Name="Button_Members" Content="Members" HorizontalAlignment="Left" Height="60" Margin="15,162,0,0" VerticalAlignment="Top" Width="119" Foreground="#FF00E8FF" BorderThickness="0" SnapsToDevicePixels="True" FontFamily="Century Gothic" FontWeight="Light" FontSize="14" BorderBrush="{Null}" Background="{Null}"/>
    <Button Name="Button_Device" Content="Device" HorizontalAlignment="Left" Height="60" VerticalAlignment="Top" Width="119" Foreground="#FF00E8FF" BorderThickness="0" SnapsToDevicePixels="True" FontFamily="Century Gothic" FontWeight="Light" FontSize="14" BorderBrush="{Null}" Background="{Null}" />
    <Button Name="Button___Permissions" Content="Permissions" HorizontalAlignment="Left" Height="60" Margin="10,73,0,0" VerticalAlignment="Top" Width="119" Foreground="#FF00E8FF" BorderThickness="0" SnapsToDevicePixels="True" FontFamily="Century Gothic" FontWeight="Light" FontSize="14" BorderBrush="{Null}" Background="{Null}"/>
    <Label Name="Label_members" Content="Geben Sie die gewünschte Gruppe ein, für welche Sie eine Liste erstellen wollen:" HorizontalAlignment="Left" Margin="160,92,0,0" VerticalAlignment="Top" FontFamily="Century Gothic"/>
    <TextBox Name="Box_members" HorizontalAlignment="Left" Height="23" Margin="160,122,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="233" MaxLines="1" FontFamily="Century Gothic"/>
    <Button Name="Button_search_members" Content="Suchen" HorizontalAlignment="Left" Height="46" Margin="160,361,0,0" VerticalAlignment="Top" Width="118" Background="#FF6B7B9E" FontFamily="Century Gothic"/>
    <Label Name="Label_device" Content="Geben Sie den Computernamen ein, für welchen Sie den Standort herausfinden möchten:" HorizontalAlignment="Left" Margin="160,92,0,0" VerticalAlignment="Top" FontFamily="Century Gothic"/>
    <TextBox Name="Box_device" HorizontalAlignment="Left" Height="23" Margin="160,122,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="233" MaxLines="1" FontFamily="Century Gothic"/>
    <Button Name="Button_search_device" Content="Suchen" HorizontalAlignment="Left" Height="46" Margin="160,361,0,0" VerticalAlignment="Top" Width="118" Background="#FF6B7B9E" FontFamily="Century Gothic"/>
    <Label Name="Label_permissions" Content="Von welchem User möchten Sie die Rechte nehmen:" HorizontalAlignment="Left" Margin="160,92,0,0" VerticalAlignment="Top" FontFamily="Century Gothic"/>
    <TextBox Name="Box_per_set" HorizontalAlignment="Left" Height="23" Margin="160,122,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="233" MaxLines="1" FontFamily="Century Gothic"/>
    <Label Name="Label_permissions2" Content="Welchem User möchten Sie die Rechte geben:" HorizontalAlignment="Left" Margin="160,162,0,0" VerticalAlignment="Top" FontFamily="Century Gothic"/>
    <TextBox Name="Box_per_get" HorizontalAlignment="Left" Height="23" Margin="160,199,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="233" MaxLines="1" FontFamily="Century Gothic"/>
    <Button Name="Button_search_1to1" Content="1:1" HorizontalAlignment="Left" Height="46" Margin="160,361,0,0" VerticalAlignment="Top" Width="118" Background="#FF6B7B9E" FontFamily="Century Gothic"/>
    <Button Name="Button_search_add" Content="Hinzufügen" HorizontalAlignment="Left" Height="46" Margin="333,361,0,0" VerticalAlignment="Top" Width="118" Background="#FF6B7B9E" FontFamily="Century Gothic"/>

</Grid>

我测试了另一个 xaml 代码,它运行良好,所以可能与 .netframework 或 PowerShell 脚本无关?

但我的脚本看起来像这样:

#==============================================================================================
# XAML Code - Imported from Visual Studio Express WPF Application
#==============================================================================================
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = @'

  [XAML CODE HERE]

'@
#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml) 
try{$Form=[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}

#===========================================================================
# Store Form Objects In PowerShell
#===========================================================================
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}


#===========================================================================
# Shows the form
#===========================================================================
$Form.ShowDialog() | out-null

我使用 Visual Studio 2017 生成了 XAML,但我认为这并不重要...

非常感谢。

问候, 罗宾 ;-)

【问题讨论】:

  • won't work - 你必须更具体 - 什么不起作用,它应该以什么方式起作用?你的问题都不清楚。
  • 您可以简单地调用: $Form=[Windows.Markup.XamlReader]::Load( $reader ) 并查看您在 XAML 解析中的异常... t 帮助您调试
  • 删除所有像这样设置的属性:="{Null}",或为它们分配一个有效值。

标签: wpf powershell xaml user-interface


【解决方案1】:

您的 XML 无效。一方面,您不能将元素分配给 Null。另外,你错过了结束&lt;/Window&gt;

我稍微清理了您的 XML。它应该在 Powershell 中加载

[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
#Build the GUI
[xml]$xaml = @"
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
    Title="Service Desk Tools" Height="450" Width="800" WindowStyle="None" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Border Name="Content" BorderBrush="Black" BorderThickness="0" HorizontalAlignment="Left" Height="412" Margin="139,38,0,0" VerticalAlignment="Top" Width="661" Background="#FFECF0F3"/>
        <Border Name="Nav" BorderBrush="Black" BorderThickness="0" HorizontalAlignment="Left" Height="450" VerticalAlignment="Top" Width="139" Background="#FF334351">
            <Border.Effect>
                <DropShadowEffect BlurRadius="16" Direction="361" RenderingBias="Quality" Color="#FFC5BEBE"/>
            </Border.Effect>
        </Border>
        <Border Name="Top" BorderBrush="Black" BorderThickness="0" HorizontalAlignment="Left" Height="38" Margin="139,0,0,0" VerticalAlignment="Top" Width="661" Background="#FF6B7B9E">
            <Border.Effect>
                <DropShadowEffect BlurRadius="16" Direction="1" RenderingBias="Quality" Color="#FFC5BEBE"/>
            </Border.Effect>
            <Button Name="Button_close" Content="X" HorizontalAlignment="Left" Height="38" Margin="615,0,0,0" VerticalAlignment="Top" Width="46" Foreground="White" FontFamily="Century Gothic" FontSize="16" FontWeight="Light" />
        </Border>
        <Button Name="Button_Members" Content="Members" HorizontalAlignment="Left" Height="60" Margin="15,162,0,0" VerticalAlignment="Top" Width="119" Foreground="#FF00E8FF" BorderThickness="0" SnapsToDevicePixels="True" FontFamily="Century Gothic" FontWeight="Light" FontSize="14" />
        <Button Name="Button_Device" Content="Device" HorizontalAlignment="Left" Height="60" VerticalAlignment="Top" Width="119" Foreground="#FF00E8FF" BorderThickness="0" SnapsToDevicePixels="True" FontFamily="Century Gothic" FontWeight="Light" FontSize="14" />
        <Button Name="Button___Permissions" Content="Permissions" HorizontalAlignment="Left" Height="60" Margin="10,73,0,0" VerticalAlignment="Top" Width="119" Foreground="#FF00E8FF" BorderThickness="0" SnapsToDevicePixels="True" FontFamily="Century Gothic" FontWeight="Light" FontSize="14" />
        <Label Name="Label_members" Content="Geben Sie die gewünschte Gruppe ein, für welche Sie eine Liste erstellen wollen:" HorizontalAlignment="Left" Margin="160,92,0,0" VerticalAlignment="Top" FontFamily="Century Gothic"/>
        <TextBox Name="Box_members" HorizontalAlignment="Left" Height="23" Margin="160,122,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="233" MaxLines="1" FontFamily="Century Gothic"/>
        <Button Name="Button_search_members" Content="Suchen" HorizontalAlignment="Left" Height="46" Margin="160,361,0,0" VerticalAlignment="Top" Width="118" Background="#FF6B7B9E" FontFamily="Century Gothic"/>
        <Label Name="Label_device" Content="Geben Sie den Computernamen ein, für welchen Sie den Standort herausfinden möchten:" HorizontalAlignment="Left" Margin="160,92,0,0" VerticalAlignment="Top" FontFamily="Century Gothic"/>
        <TextBox Name="Box_device" HorizontalAlignment="Left" Height="23" Margin="160,122,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="233" MaxLines="1" FontFamily="Century Gothic"/>
        <Button Name="Button_search_device" Content="Suchen" HorizontalAlignment="Left" Height="46" Margin="160,361,0,0" VerticalAlignment="Top" Width="118" Background="#FF6B7B9E" FontFamily="Century Gothic"/>
        <Label Name="Label_permissions" Content="Von welchem User möchten Sie die Rechte nehmen:" HorizontalAlignment="Left" Margin="160,92,0,0" VerticalAlignment="Top" FontFamily="Century Gothic"/>
        <TextBox Name="Box_per_set" HorizontalAlignment="Left" Height="23" Margin="160,122,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="233" MaxLines="1" FontFamily="Century Gothic"/>
        <Label Name="Label_permissions2" Content="Welchem User möchten Sie die Rechte geben:" HorizontalAlignment="Left" Margin="160,162,0,0" VerticalAlignment="Top" FontFamily="Century Gothic"/>
        <TextBox Name="Box_per_get" HorizontalAlignment="Left" Height="23" Margin="160,199,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="233" MaxLines="1" FontFamily="Century Gothic"/>
        <Button Name="Button_search_1to1" Content="1:1" HorizontalAlignment="Left" Height="46" Margin="160,361,0,0" VerticalAlignment="Top" Width="118" Background="#FF6B7B9E" FontFamily="Century Gothic"/>
        <Button Name="Button_search_add" Content="Hinzufügen" HorizontalAlignment="Left" Height="46" Margin="333,361,0,0" VerticalAlignment="Top" Width="118" Background="#FF6B7B9E" FontFamily="Century Gothic"/>

    </Grid>
</Window>
"@ 

#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml) 
$Form=[Windows.Markup.XamlReader]::Load( $reader )
<#
try{}
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}
#>
#===========================================================================
# Store Form Objects In PowerShell
#===========================================================================
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}


#===========================================================================
# Shows the form
#===========================================================================
$Form.ShowDialog() | out-null

【讨论】:

  • 有道理,非常感谢!我实际上对 PowerShell 很陌生:P
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-29
  • 2018-10-17
  • 2017-09-21
  • 2016-09-28
  • 1970-01-01
  • 2023-01-14
  • 1970-01-01
相关资源
最近更新 更多