I’m trying to run default code after adding WPF window in VisualStudio 2019
Microsoft Visual Studio Community 2019
Version 16.4.2
script:
try
{
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”>
<Grid>
<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 ve found the answer on this issue
In settings PoshTools it must be set “Single Threaded Apartment State”
adam
January 13, 2020, 4:53pm
4
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?
adam
January 29, 2020, 9:29pm
6
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]
[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
adam
February 3, 2020, 2:26pm
11
What if you change this line:
[Windows.Markup.XamlReader]::Load($xamlReader)
To
[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.
OK tried a couple of things.
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
seem this line is the key
[System.Reflection.Assembly]::LoadWithPartialName(‘PresentationFramework’) | Out-Null
Shawn
March 27, 2020, 6:10pm
15
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.