I’m trying to run default code after adding WPF window in VisualStudio 2019
Microsoft Visual Studio Community 2019
Version 16.4.2
script:
Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,system.windows.forms
catch
Throw "Failed to load Windows Presentation Framework assemblies."
function Load-Xaml {
[xml]$xaml = Get-Content -Path $PSScriptRoot\WpfWindow1.xaml
$manager = New-Object System.Xml.XmlNamespaceManager -ArgumentList $xaml.NameTable
$manager.AddNamespace(“x”, “
http://schemas.microsoft.com/winfx/2006/xaml
”);
$xamlReader = New-Object System.Xml.XmlNodeReader $xaml
[Windows.Markup.XamlReader]::Load($xamlReader)
$window = Load-Xaml
$window.ShowDialog()
Error occurs while executes this line:
[Windows.Markup.XamlReader]::Load($xamlReader)
sorry it is in russian lang …
[ERROR] Исключение при вызове “Load” с “1” аргументами: “Вызов конструктора для типа “System.Windows.
[ERROR] Window”, удовлетворяющего указанным ограничениям привязки, привел к выдаче исключения.”
[ERROR] C:\Users\superuser\source\repos\PowerShellProject1\PowerShellProject1\WpfWindow1.xaml.ps1:15
[ERROR] знак:2
[ERROR] + [Windows.Markup.XamlReader]::Load($xamlReader)
[ERROR] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[ERROR] + CategoryInfo : NotSpecified: (
[], MethodInvocationException
[ERROR] + FullyQualifiedErrorId : XamlParseException
[ERROR]
[ERROR] Невозможно вызвать метод для выражения со значением NULL.
[ERROR] C:\Users\superuser\source\repos\PowerShellProject1\PowerShellProject1\WpfWindow1.xaml.ps1:19
[ERROR] знак:1
[ERROR] + $window.ShowDialog()
[ERROR] + ~~~~~~~~~~~~~~~~~~~~
[ERROR] + CategoryInfo : InvalidOperation: (
[], RuntimeException
[ERROR] + FullyQualifiedErrorId : InvokeMethodOnNull
[ERROR]
this script runs exelent in PS ISE …
more info about error
Error occurs during this line of code only in Visual Studio 2019. VS Code an powershell ISE runs it successfully.
this line of code:
$err.CategoryInfo
Category : NotSpecified
Activity :
Reason : MethodInvocationException
TargetName :
TargetType :
$err.FullyQualifiedErrorId
XamlParseException
$err.InvocationInfo
MyCommand :
BoundParameters : {}
UnboundArguments : {}
ScriptLineNumber : 6
OffsetInLine : 2
HistoryId : -1
ScriptName : C:\Users\anton\source\repos\TestPS\WpfWindow1.xaml.ps1
Line :
Windows.Markup.XamlReader
::Load($xamlReader)
PositionMessage : C:\Users\anton\source\repos\TestPS\WpfWindow1.xaml.ps1:6 знак:2
+
Windows.Markup.XamlReader
::Load($xamlReader)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PSScriptRoot : C:\Users\anton\source\repos\TestPS
PSCommandPath : C:\Users\anton\source\repos\TestPS\WpfWindow1.xaml.ps1
InvocationName :
PipelineLength : 0
PipelinePosition : 0
ExpectingInput : False
CommandOrigin : Internal
DisplayScriptPosition :
$err.Exception
Исключение при вызове “Load” с “1” аргументами: “Вызов конструктора для типа “System.Windows.Window”, удовлетворяющего указанным ограничениям привязки, привел к выдаче исключения.”
translate by google:
Exception when calling “Load” with" 1 “arguments:” calling the constructor for type " System.Windows.Window " that satisfies the specified binding constraints resulted in an exception."
xaml (default after add wpf window in project):
<Window
xmlns=…
xmlns:x=…
Title=“MainWindow” Height=“350” Width=“525”>
<Label Content="Label" HorizontalAlignment="Left" Margin="68,38,0,0" VerticalAlignment="Top" Width="197"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="307,41,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
i think i was getting this same error and have ticked the “single Threaded Apartment state”
but now i get
The program ‘WpfWindow1.xaml.ps1: PowerShell Script’ has exited with code 0 (0x0).
but nothing appears?
this applies even when i dont actually make any change and it just loads th edefault wpf
any suggestions?
1 Try set STA
2 when change powershell host ( for example from x64 to 32 bit version)
3 close VS
4 restart VS
5 wait PS host to completly load
6 check sta is On
7 try run your scrips …
full error:
[ERROR] Unable to find type [Windows.Markup.XamlReader].
[ERROR] At E:\VisualStudio\New folder\PowerShellProject1\WpfWindow1.xaml.ps1:6 char:2
[ERROR] + [Windows.Markup.XamlReader]::Load($xamlReader)
[ERROR] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~
[ERROR] + CategoryInfo : InvalidOperation: (Windows.Markup.XamlReader:Typ
[ERROR] eName) [], RuntimeException
[ERROR] + FullyQualifiedErrorId : TypeNotFound
[ERROR]
[ERROR] You cannot call a method on a null-valued expression.
[ERROR] At E:\VisualStudio\New folder\PowerShellProject1\WpfWindow1.xaml.ps1:10 char:1
[ERROR] + $window.ShowDialog()
[ERROR] + ~~~~~~~~~~~~~~~~~~~~
[ERROR] + CategoryInfo : InvalidOperation: (
[], RuntimeException
[ERROR] + FullyQualifiedErrorId : InvokeMethodOnNull
[ERROR]
The program ‘WpfWindow1.xaml.ps1: PowerShell Script’ has exited with code 0 (0x0).
Opens fine in ISE
[SYstem.Windows.Markup.XamlReader]::Load($xamlReader)
Can you also try putting this at the top of your script?
[System.Reflection.Assembly]::LoadWithPartialName('PresentationFramework') | Out-Null
It looks like it’s not loading WPF for some reason.
created new solution, as is, tried to run got error.
Made change to [Windows.Markup.XamlReader]::Load($xamlReader) and it run.
Changed back to how it was…still run
created new solution run as is and worked.
exited vs, restarted, created new solution, didnt work.
Add line [System.Reflection.Assembly]::LoadWithPartialName(‘PresentationFramework’) | Out-Null
while leaving other line as default…worked
and tried another test by opening one id created and started work on wheni discovered problem.
Made both changes and neither work.
get the “The program ‘WpfWindow1.xaml.ps1: PowerShell Script’ has exited with code 0 (0x0).”
message
I experienced this same issue when going through the tutorial here:
https://docs.poshtools.com/powershell-pro-tools-documentation/visual-studio/user-interface-design/building-a-gui-with-windows-forms-in-visual-studio
You may want to update that, because it may cause frustration for people that are testing the software like I was.
Thanks for this thread, it did help solve the issue for me.