Reputation: 199
I try to display a little XAML form which was created on visual studio 2019(application WPF .NET framework project).But it doesn't work I have the 2 errors:
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.
Do you have a solution please? Here is my code on my powershell file .ps1, I have added the XAML code in my powershell file :
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
Upvotes: 2
Views: 2013
Reputation: 199
I delete manually this line, and it works now:
mc:Ignorable="d"
In somes sites, they have this code to delete automaticallay the xaml code. here is the code:
$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
Upvotes: 3
Reputation: 3168
I'd suggest some error checking as follows:
#Load required libraries
$TermMsg = {
[Windows.Forms.MessageBox]::Show($Message,
"Program Terminated:",
[Windows.Forms.MessageBoxButtons]::OK ,
[Windows.Forms.MessageBoxIcon]::Information) | Out-Null}
Add-Type -AssemblyName "System.Windows.Forms"
Add-Type -AssemblyName "System.io"
If ($host.Name -eq 'ConsoleHost' -or
$host.Name -eq 'Visual Studio Code Host') {
try{ <#+------------------------------------------------+
| Check that the proper assemblies are loaded |
| Required for PS Console and Visual Code, while |
| only Systems.Windows.Forms needed for PSISE! |
+------------------------------------------------+
#>
$ATArgs = @{AssemblyName = "PresentationCore",
"PresentationFramework",
"WindowsBase"
ErrorAction = 'Stop'}
Add-Type @ATArgs
}
catch {
$Message =
"Failed to load Windows Presentation Framework" +
" and/or other assemblies required for this program!"
& $TermMsg
Exit
}
} #End If ($host...
[xml]$xaml = @"
<Window
xmlns =
"http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
Name = "Window"
Title = "Test Form"
WindowStartupLocation = "CenterScreen"
Width = "450"
Height = "800"
FontSize = "18"
Background = "Blue"
Foreground = "White"
ShowInTaskbar = "True">
<Grid>
<TextBox x:Name="tboxVerInfo"
Width="400"
Height="30"
HorizontalAlignment="Left"
VerticalAlignment ="Top"
Margin = "10,10,5,0" />
</Grid>
</Window>
"@
$NOArgs = @{TypeName = 'System.Xml.XmlNodeReader'
ArgumentList = $xaml}
$reader=(New-Object @NOArgs)
Try {$MainForm=[Windows.Markup.XamlReader]::Load( $reader )
$MainForm.ShowDialog() | Out-Null}
Catch{Write-Host $("Unable to load Windows.Markup.XamlReader." +
"`nDouble-check syntax and ensure " +
".net is installed.")
}
Note: this makes sure the proper assemblies are loaded for the environment you are using, i.e. ISE,PS Console, VSCode. HTH
Upvotes: 2