gpt4 book ai didi

wpf - 使用 powershell 显示一个小 wpf 表单

转载 作者:行者123 更新时间:2023-12-02 02:23:31 26 4
gpt4 key购买 nike

我尝试显示在 Visual Studio 2019(应用程序 WPF .NET 框架项目)上创建的一个小 XAML 表单。但它不起作用我有两个错误:

Line |
25 | $MainForm = [Windows.Markup.XamlReader]::Load($Reader)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Exception calling "Load" with "1" argument(s): "Cannot set unknown member '{http://schemas.openxmlformats.org/markup-compatibility/2006}Ignorable'."


Line |
30 | $MainForm.ShowDialog() | Out-Null
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| You cannot call a method on a null-valued expression.

请问有解决办法吗?这是我的 powershell 文件 .ps1 上的代码,我已在我的 powershell 文件中添加了 XAML 代码:

Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms

[XML]$XAML =
@'
<Window x:Class="WpfApp5.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:WpfApp5"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid/>
</Window>
'@


$XAML.Window.RemoveAttribute("x:Class")
$Reader = New-Object System.Xml.XmlNodeReader $XAML
$MainForm = [Windows.Markup.XamlReader]::Load($Reader)
$MainForm.ShowDialog() | Out-Null

最佳答案

我手动删除了这一行,现在它可以工作了:

mc:Ignorable="d"

在某些网站中,他们有此代码来自动删除 xaml 代码。这是代码:

$inputXAMLClean = $inputXAML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace 'x:Class=".*?"','' -replace 'd:DesignHeight="\d*?"','' -replace 'd:DesignWidth="\d*?"','



But when I try to add the code:
$inputXAMLClean = $inputXAML -replace 'mc:Ignorable="d"',''

I have the error:

Line |
31 | $Reader = New-Object System.Xml.XmlNodeReader $XAML1
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Cannot convert argument "0", with value: "System.Xml.XmlDocument", for "XmlNodeReader" to type "System.Xml.XmlNode": "Cannot convert the "System.Xml.XmlDocument" value
| of type "System.String" to type "System.Xml.XmlNode"."



Here is the good code,it works:


Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms

[XML]$XAML =
@'
<Window x:Class="WpfApp5.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:WpfApp5"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Label Content="Label" HorizontalAlignment="Left" Margin="280,179,0,0" VerticalAlignment="Top"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="457,136,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>

'@


$XAML.Window.RemoveAttribute("x:Class")
$XAML.Window.RemoveAttribute("mc:Ignorable")
$Reader = New-Object System.Xml.XmlNodeReader $XAML
$MainForm = [Windows.Markup.XamlReader]::Load($Reader)
$MainForm.ShowDialog() | Out-Null

关于wpf - 使用 powershell 显示一个小 wpf 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66079840/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com