Wpf 使Visual Studio XAML输出与System.Windows.Markup.XamlReader兼容
我正试图在Visual Studio(2015)中创建一个WPF GUI,并通过Wpf 使Visual Studio XAML输出与System.Windows.Markup.XamlReader兼容,wpf,visual-studio,xaml,powershell,Wpf,Visual Studio,Xaml,Powershell,我正试图在Visual Studio(2015)中创建一个WPF GUI,并通过[System.Windows.Markup.XamlReader]::load()方法将创建的XAML加载到Powershell 问题是,一些基本控件还可以(在进行了一些替换之后),但只要在VS中配置更多属性,在使用XAMLreader加载XAML时就会出现无数错误。 示例:这个问题的答案在VisualStudio中运行良好,但在通过Xamlreader加载时会产生很多错误 那么,为什么它不起作用?System.
[System.Windows.Markup.XamlReader]::load()
方法将创建的XAML
加载到Powershell
问题是,一些基本控件还可以(在进行了一些替换之后),但只要在VS中配置更多属性,在使用XAMLreader加载XAML时就会出现无数错误。示例:这个问题的答案在VisualStudio中运行良好,但在通过Xamlreader加载时会产生很多错误
- 那么,为什么它不起作用?
是 遵循与VS生成的XAML相同的shema (至少它在标题中说明了这一点)System.Windows.Markup.XamlReader
- 如何使VS生成的XAML与 XamlReader
- 如果不可能,是否有另一种方法加载VS 将XAML生成到Powershell中
# ~~~~~~~~~~~~ WPF ~~~~~~~~~~~~~
$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:Window3"
Title="Window" Height="40" Width="40" ToolTip="Tooltip" Topmost="True" WindowStyle="None" AllowsTransparency="True" Background="Transparent" ResizeMode="CanResizeWithGrip" MaxWidth="100" MaxHeight="100" MinWidth="20" MinHeight="20">
<Border BorderBrush="#FF000000" BorderThickness="1,1,1,1" CornerRadius="5,5,5,5" UseLayoutRounding="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label x:Name="Backdrop" Grid.ColumnSpan="2" Content="Label" Margin="0,0,0,0" Foreground="{x:Null}" Background="#FFAD3838"/>
<Button x:Name="Button1" Grid.Column="0" Content="" Margin="1" BorderThickness="0" Background="#FF3B87BD"/>
<Button x:Name="Button2" Grid.Column="1" Content="" Margin="1" BorderThickness="0" Background="#FF59B483"/>
</Grid>
</Border>
</Window>
"@
# Add Type
Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,system.windows.forms
# read XAML
$inputXML = $Xaml -replace 'mc:Ignorable="d"','' -replace "x:N",'N'
[XML]$script:WpfXml = $inputXML
# Remove Class, Load Reader
$WpfXml.Window.RemoveAttribute(“x:Class”)
$Reader = New-Object System.Xml.XmlNodeReader $WpfXml
$WpfForm = [Windows.Markup.XamlReader]::Load($Reader)
$WpfXml.SelectNodes("//*[@Name]") | %{ Set-Variable -Name ($_.Name) -Value $WpfForm.FindName($_.Name) -Scope script }
$WpfForm.showdialog()
#~~~~~~~~~~~~~WPF~~~~~~~~~~~~~
$Xaml=@”
"@
#添加类型
添加类型-AssemblyName PresentationCore、PresentationFramework、WindowsBase、system.windows.forms
#读取XAML
$inputXML=$Xaml-替换为'mc:Ignorable=“d',''-替换为'x:N','N'
[XML]$script:WpfXml=$inputXML
#删除类,加载读取器
$WpfXml.Window.RemoveAttribute(“x:Class”)
$Reader=New Object System.Xml.XmlNodeReader$WpfXml
$WpfForm=[Windows.Markup.XamlReader]::加载($Reader)
$WpfXml.SelectNodes(“//*[@Name]”)|%{Set Variable-Name($\ux.Name)-Value$WpfForm.FindName($\ux.Name)-Scope script}
$WpfForm.showdialog()
如评论中所述,您给出的示例很容易适用于非VS运行时
x:Class
属性,因为我们没有定义相应的类mc:Ignorable
属性,因为它无法解析$null=[System.Reflection.Assembly]::LoadWithPartialName('presentationframework'))
$NodeReader=New Object System.Xml.XmlNodeReader$([Xml]@'
'@)
$Window=[System.Windows.Markup.XamlReader]::加载($NodeReader)
$Window.ShowDialog()
为完整起见,请参阅您链接到的问题答案中的示例:
$null = [System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
$NodeReader = New-Object System.Xml.XmlNodeReader $([xml]@'
<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:ExampleWin"
Title="Window" Height="200" Width="200" ToolTip="Tooltip" Topmost="True" WindowStyle="None" AllowsTransparency="True" Background="Transparent" ResizeMode="CanResizeWithGrip">
<Border BorderBrush="#FF000000" BorderThickness="1,1,1,1" CornerRadius="5,5,5,5" UseLayoutRounding="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label x:Name="Backdrop" Grid.ColumnSpan="2" Content="Label" Margin="0,0,0,0" Foreground="{x:Null}" Background="#FFAD3838"/>
<Button x:Name="Button1" Grid.Column="0" Content="" Margin="1" BorderThickness="0" Background="#FF3B87BD"/>
<Button x:Name="Button2" Grid.Column="1" Content="" Margin="1" BorderThickness="0" Background="#FF59B483"/>
</Grid>
</Border>
</Window>
'@)
$Window = [System.Windows.Markup.XamlReader]::Load($NodeReader)
$Window.ShowDialog()
$null=[System.Reflection.Assembly]::LoadWithPartialName('presentationframework'))
$NodeReader=New Object System.Xml.XmlNodeReader$([Xml]@'
'@)
$Window=[System.Windows.Markup.XamlReader]::加载($NodeReader)
$Window.ShowDialog()
到底是哪些错误?我希望类似于
x:Class
的引用会抛出错误,因为PowerShell没有关联的Classic说“让它工作”然后说“哪种特定错误无关紧要”是没有意义的。我们只能修复您告诉我们的错误。@Moss删除x:Class
,mc:Ignorable
属性并关闭窗口
标记,如果您没有从答案中获取代码。正如我所说,随着xaml复杂性的增加,问题变得更加严重。由于-replace'x:N','N'
,您正在破坏名称属性的名称空间引用和它的{x:Null}
值引用。似乎我对许多错误的印象只是由于指向错误方向的无用错误消息。再次感谢并为我的无知感到抱歉。@Moss不用担心,很乐意帮忙:-)
$null = [System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
$NodeReader = New-Object System.Xml.XmlNodeReader $([xml]@'
<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:ExampleWin"
Title="Window" Height="200" Width="200" ToolTip="Tooltip" Topmost="True" WindowStyle="None" AllowsTransparency="True" Background="Transparent" ResizeMode="CanResizeWithGrip">
<Border BorderBrush="#FF000000" BorderThickness="1,1,1,1" CornerRadius="5,5,5,5" UseLayoutRounding="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label x:Name="Backdrop" Grid.ColumnSpan="2" Content="Label" Margin="0,0,0,0" Foreground="{x:Null}" Background="#FFAD3838"/>
<Button x:Name="Button1" Grid.Column="0" Content="" Margin="1" BorderThickness="0" Background="#FF3B87BD"/>
<Button x:Name="Button2" Grid.Column="1" Content="" Margin="1" BorderThickness="0" Background="#FF59B483"/>
</Grid>
</Border>
</Window>
'@)
$Window = [System.Windows.Markup.XamlReader]::Load($NodeReader)
$Window.ShowDialog()