【问题标题】:XAML WPF GUI in PowershellPowershell 中的 XAML WPF GUI
【发布时间】:2016-12-07 07:37:42
【问题描述】:

我目前正在构建一个将操作系统信息提取到 GUI 中的 powershell 程序。我目前对我认为无效的 xaml 代码有疑问 -

[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"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="OS Details" Height="384.399" Width="525">
<Grid Margin="0,0,0,-12">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="281*"/>
        <ColumnDefinition Width="236*"/>
    </Grid.ColumnDefinitions>
    <TextBox Name="textBox" Height="37" Margin="0,1,0,0" TextWrapping="Wrap"    Text=" &#x9;               Operating System Details" VerticalAlignment="Top" FontSize="18" Background="#FF9BABF1" HorizontalAlignment="Left" Width="517" Grid.ColumnSpan="2"/>
    <Label Name="HostName" Content="HostName" HorizontalAlignment="Left" Height="35" Margin="0,43,0,0" VerticalAlignment="Top" Width="246" Background="#FF9BABF1"/>
    <Label Name="OSName" Content="OS Name" HorizontalAlignment="Left" Height="35" Margin="0,83,0,0" VerticalAlignment="Top" Width="246" Background="#FF9BABF1"/>
    <Label Name="AvailableMemory" Content="Available Memory" HorizontalAlignment="Left" Height="35" Margin="0,123,0,0" VerticalAlignment="Top" Width="246" Background="#FF9BABF1"/>
    <Label Name="OSArchitecture" Content="OS Architecture" HorizontalAlignment="Left" Height="35" Margin="0,163,0,0" VerticalAlignment="Top" Width="246" Background="#FF9BABF1"/>
    <Label Name="WindowsDirectory" Content="Windows Directory" HorizontalAlignment="Left" Height="35" Margin="0,203,0,0" VerticalAlignment="Top" Width="246" Background="#FF9BABF1"/>
    <Label Name="WindowsVersion" Content="Windows Version" HorizontalAlignment="Left" Height="33" Margin="0,243,0,0" VerticalAlignment="Top" Width="246" Background="#FF9BABF1"/>
    <Label Name="SystemDrive" Content="System Drive" HorizontalAlignment="Left" Height="32" Margin="0,281,0,0" VerticalAlignment="Top" Width="246" Background="#FF9BABF1"/>
    <TextBox Name="txtHostname" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="35" Margin="253,43,0,0" TextWrapping="Wrap" Text="&#xD;&#xA;" VerticalAlignment="Top" Width="254" TextChanged="textBox1_TextChanged"/>
    <TextBox Name="txtOSName" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="35" Margin="253,83,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="254"/>
    <TextBox Name="txtAvailMem" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="35" Margin="253,123,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="254"/>
    <TextBox Name="txtOSArch" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="35" Margin="253,163,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="254"/>
    <TextBox Name="txtWinDir" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="35" Margin="253,203,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="254"/>
    <TextBox Name="txtWinVer" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="35" Margin="253,241,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="254"/>
    <TextBox Name="txtSysDrv" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="32" Margin="253,281,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="254"/>
    <Button Name="button" Grid.ColumnSpan="2" Content="Exit" HorizontalAlignment="Left" Height="27" Margin="10,318,0,0" VerticalAlignment="Top" Width="490"/>

</Grid>
</Window>

'@

#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}

当我运行它时,我得到“catch”语句(代码是由另一个用户在另一个有用的站点上编写的 - https://blogs.technet.microsoft.com/platformspfe/2014/01/20/integrating-xaml-into-powershell/#comment-3105

关于我哪里出错了有什么想法吗?

提前致谢!

【问题讨论】:

    标签: xaml powershell


    【解决方案1】:

    当我尝试在实际 WPF 项目中使用此 xaml 代码时,我在构建时收到以下错误:

    错误“窗口”根元素需要 x:Class 属性来支持 XAML 文件中的事件处理程序。删除事件处理程序 TextChanged 事件,或将 x:Class 属性添加到根元素。 第 20 行位置 190

    这是问题所在:

    <TextBox Name="txtHostname" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="35" Margin="253,43,0,0" TextWrapping="Wrap" Text="&#xD;&#xA;" VerticalAlignment="Top" Width="254" TextChanged="textBox1_TextChanged"/>
    

    如果您不需要该 TextChanged 事件,请尝试将其删除,看看是否能解决问题。

    【讨论】:

      【解决方案2】:

      TextChanged="textBox1_TextChanged" 是问题所在,没有 textBox1 导致事件无法生成。不过,您应该能够安全地删除它,一旦将控件加载到脚本中,您就可以在代码中定义 $txtHostName.Add_TextChanged

      【讨论】:

        【解决方案3】:

        正如 Asnivor 所说,您不能像这样在您的 xaml 中使用事件。

        一种选择是从 xaml 中取出 TextChanged="textBox1_TextChanged" 并将事件添加到您的代码后面。

        $txtHostname.add_textchanged({
        
            Your code here
        
        })
        

        【讨论】:

          猜你喜欢
          • 2017-10-08
          • 2018-09-27
          • 1970-01-01
          • 2016-09-28
          • 1970-01-01
          • 2015-07-13
          • 2019-10-29
          • 2018-10-17
          • 1970-01-01
          相关资源
          最近更新 更多