我目前正在构建一个将操作系统信息提取到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=" 	 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="
" 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)
关于我哪里出错的任何想法?
提前致谢!
答案 0 :(得分:1)
当我尝试在实际的WPF项目中使用此xaml代码时,我在构建时遇到以下错误:
错误&#39;窗口&#39; root元素需要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="
" VerticalAlignment="Top" Width="254" TextChanged="textBox1_TextChanged"/>
如果你不需要那个TextChanged事件,请尝试删除它,看看是否能解决问题。
答案 1 :(得分:0)
TextChanged="textBox1_TextChanged"
是问题,没有textBox1,因此事件无法生成。您应该能够安全地删除它,一旦将控件加载到脚本中,您就可以在代码中定义$txtHostName.Add_TextChanged
。
答案 2 :(得分:0)
正如Asnivor所说,你不能像你那样在你的xaml中使用一个事件。
一种选择是取出TextChanged =&#34; textBox1_TextChanged&#34;从xaml中将事件添加到后面的代码中。
$txtHostname.add_textchanged({
Your code here
})