PowerShell中的XAML无法在一行中编写xmlns模式

时间:2018-02-08 08:12:25

标签: powershell xaml

我尝试在PowerShell中使用XAML并遇到一个奇怪的问题。如果我在下面的一行中写下$inputXML = @" <Window x:Class="WpfApplication2.MainWindow" 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:WpfApplication2" mc:Ignorable="d" Title="test" Height="416.794" Width="598.474" Topmost="True"> <Grid Margin="0,0,45,0"> <Image x:Name="image" HorizontalAlignment="Left" Height="100" Margin="24,28,0,0" VerticalAlignment="Top" Width="100" Source="C:\Users\Stephen\Dropbox\Docs\blog\foxdeploy favicon.png"/> <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Height="100" Margin="174,28,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="282" FontSize="16"><Run Text="Use this tool to find disk information"/><InlineUIContainer> <TextBlock x:Name="textBlock1" TextWrapping="Wrap" Text="TextBlock"/> </InlineUIContainer></TextBlock> <Button x:Name="button" Content="get-DiskInfo" HorizontalAlignment="Left" Height="35" Margin="393,144,0,0" VerticalAlignment="Top" Width="121" FontSize="18.667"/> <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="35" Margin="186,144,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="168" FontSize="16"/> <Label x:Name="label" Content="ComputerName" HorizontalAlignment="Left" Height="46" Margin="24,144,0,0" VerticalAlignment="Top" Width="138" FontSize="16"/> <ListView x:Name="listView" HorizontalAlignment="Left" Height="156" Margin="24,195,0,0" VerticalAlignment="Top" Width="511" FontSize="16"> <ListView.View> <GridView> <GridViewColumn Header="Drive Letter" DisplayMemberBinding ="{Binding 'Drive Letter'}" Width="120"/> <GridViewColumn Header="Drive Label" DisplayMemberBinding ="{Binding 'Drive Label'}" Width="120"/> <GridViewColumn Header="Size(MB)" DisplayMemberBinding ="{Binding Size(MB)}" Width="120"/> <GridViewColumn Header="FreeSpace%" DisplayMemberBinding ="{Binding FreeSpace%}" Width="120"/> </GridView> </ListView.View> </ListView> </Grid> </Window> "@

$inputXML = @"
...
"@        

$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'

[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = $inputXML
#Read XAML

$reader = (New-Object System.Xml.XmlNodeReader $xaml)
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Warning "Unable to parse XML, with error: $($Error[0])`n Ensure that there are NO SelectionChanged properties (PowerShell cannot process them)"
throw}

它会抛出错误

Exception calling "Load" with "1" argument(s): "Cannot create unknown type
'Window'."
At line:36 char:5
+ try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : XamlParseException

如果我将它们中的每一个写成如下所示,它可以正常工作。这是批次处理吗?

apply_async

我从a blog获取代码以测试兼容性:

AsyncResult

1 个答案:

答案 0 :(得分:1)

您使用的代码已损坏。撰写该博客帖子的人显然忽略了实际测试他们发布的内容,因为该代码应该抛出您观察到的相同错误。声明

$inputXML = $inputXML ... -replace '^<Win.*', '<Window'

将更改行

<Window x:Class="WpfApplication2.MainWindow" ... Topmost="True">

<Window

没有关闭角括号,因此使XML无效。

您的第二个XML示例不会受到影响,因为其<Window>标记包含在多行中,因此结束角括号位于不同的行上,因此不会受到替换操作的影响。

<Window x:Class="WpfApplication2.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
...
Title="test" Height="416.794" Width="598.474" Topmost="True">

变成

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
...
Title="test" Height="416.794" Width="598.474" Topmost="True">

仍然是有效的XML。

将模式<Win.*调整为更具体的问题,问题将消失。假设您要从代码中删除x:Class属性(因为将<Window替换为<Window并不是很有意义),您可以执行以下操作:

$inputXML = $inputXML ... -replace '(?<=<Window) x:Class=".*?"',''

顺便说一句,这是一个很好的例子,说明为什么你不应该首先使用字符串操作来操作XML(related)。一个明显更好的问题解决方案(同样,假设您要从x:Class节点中删除<Window>属性)将删除之后的 XML解析器提供的属性使用适当的工具:

$inputXML = @"
...
"@

[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = $inputXML

$XAML.Window.RemoveAttribute('x:Class')