【发布时间】:2016-04-17 21:38:10
【问题描述】:
我正在尝试在 Visual Studio (2015) 中创建一个 WPF GUI 并将创建的 XAML 加载到 Powershell 中,通过[System.Windows.Markup.XamlReader]::load() 方法。
问题是,一些基本的控件是好的(在一些替换之后),但是一旦你在 VS 中配置了更多的属性,当你用 XAMLreader 加载 XAML 时,你就会得到源源不断的错误。
示例:question 的答案在 VisualStudio 中运行良好,但在通过 Xamlreader 加载时会产生很多错误。
那么,为什么它不起作用?
System.Windows.Markup.XamlReader是 supposed 遵循与 VS 生成的 XAML 相同的 shema (至少它在其标题中说明了这一点)。如何让 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()
【问题讨论】:
-
究竟是哪些错误?我希望像
x:Class这样的引用会引发错误,因为 PowerShell 没有关联的类 -
说“让它工作”然后说“哪个特定错误都没有关系”是没有意义的。我们只能修复您告诉我们的错误。
-
@Moss 去掉
x:Class、mc:Ignorable属性并关闭Window标签,完成
标签: wpf visual-studio xaml powershell