Error Windows.Markup.XamlReader Load

I’m trying to run default code after adding WPF window in VisualStudio 2019

Microsoft Visual Studio Community 2019
Version 16.4.2

    Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,
    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”, “”);
$xamlReader = New-Object System.Xml.XmlNodeReader $xaml

$window = Load-Xaml

Error occurs while executes this line:

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: (:slight_smile: [], MethodInvocationException
[ERROR] + FullyQualifiedErrorId : XamlParseException
[ERROR] Невозможно вызвать метод для выражения со значением NULL.
[ERROR] C:\Users\superuser\source\repos\PowerShellProject1\PowerShellProject1\WpfWindow1.xaml.ps1:19
[ERROR] знак:1
[ERROR] + $window.ShowDialog()
[ERROR] + ~~~~~~~~~~~~~~~~~~~~
[ERROR] + CategoryInfo : InvalidOperation: (:slight_smile: [], RuntimeException
[ERROR] + FullyQualifiedErrorId : InvokeMethodOnNull

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:

Category : NotSpecified
Activity :
Reason : MethodInvocationException
TargetName :
TargetType :



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 :

Исключение при вызове “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):




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"/>


I ve found the answer on this issue
In settings PoshTools it must be set “Single Threaded Apartment State”

Ah, good catch. I’ll add that to the documentation and put a better error message in.

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?

Does this do the same thing when running the script from PowerShell.exe?

apologies i meant to mention that

No it runs from ise

1 Like

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 …

Sorry Kuznetcovey

But im not sure what you mean or where i would do this?

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] 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: (:slight_smile: [], RuntimeException
[ERROR] + FullyQualifiedErrorId : InvokeMethodOnNull
The program ‘WpfWindow1.xaml.ps1: PowerShell Script’ has exited with code 0 (0x0).

Opens fine in ISE

What if you change this line:




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.

OK tried a couple of things.

  1. 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

  2. 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).”


seem this line is the key

[System.Reflection.Assembly]::LoadWithPartialName(‘PresentationFramework’) | Out-Null

I experienced this same issue when going through the tutorial here:

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.