_From @music2myear on June 15, 2017 17:38_
Steps to Reproduce:
WPF code not working.
Code:
[xml]$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window" Title="Initial Window" WindowStartupLocation = "CenterScreen"
Width = "800" Height = "600" ShowInTaskbar = "True">
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load( $reader )
Output:
Unable to find type [Windows.Markup.XamlReader].
At C:\path\script.ps1:12 char:9
- $Window=[Windows.Markup.XamlReader]::Load( $reader )
~~~~~~~
- CategoryInfo : InvalidOperation: (Windows.Markup.XamlReader:TypeName) [], RuntimeException
- FullyQualifiedErrorId : TypeNotFound
Running same code in PowerShell ISE works just fine.
Before issue began, three different installs occurred on my computer:
I have confirmed that .NET 4.7 is still loaded in system.
PowerShell ISE runs same code just fine (simple window is displayed).
_Copied from original issue: Microsoft/vscode#28827_
Moving to the Powershell extension repo first to rule out issues from the extension
I'm pretty sure this works in ISE because ISE is WPF-based and it has the WPF assemblies loaded. I'm also pretty sure this wouldn't work if you ran the above directly from the PowerShell console. You need to use Add-Type to load the PresentationFramework assembly IIRC. See this SO post for more details: https://stackoverflow.com/questions/39386687/powershell-cant-load-windows-markup-xamlreader
@music2myear were you ever able to run WPF code inside of the PowerShell extension? I'd bet that it never worked before because WPF does not get loaded into the PowerShell session by default in VS Code. It's already loaded there in the PowerShell ISE because the ISE itself uses WPF for its UI.
Try adding this line before you load the XAML:
Add-Type -AssemblyName PresentationFramework
This will ensure that WPF's assemblies are loaded into the process before you try to use them.
Double the support, double the fun ;)
@daviwil Yes, prior to the updates noted in the OP I was able to run WPF code without issue. These and other tutorial scripts worked just fine pasting the code into a ps1 file in VS Code, saving, and executing in either the Powershell.exe or "PowerShell Integrated" terminals. Both worked the same and both ran WPF code.
Earlier today I thought there might have been an issue with PS on my computer and so just completed the standard Win10 corruption repair process including sfc /scannow, the full DISM health check, and a repair install when there were issues the other items were unable to resolve (checking the CBS logs showed these were not PowerShell-related, but apparently back checksums on .lnk files relevant to the RSAT tools).
Checking post-repair install I'm having the same issue.
All that out of the way, adding the Add-Type line you referenced appears to have done the trick.
I know this wasn't being reference in my PS profiles, so I'm not sure how it was working before without this line being in the scripts somewhere, but they were.
As these scripts are intended for use somewhere besides my own computer, it makes sense for this line to be in the code, so perhaps having this problem now and you getting me the solution means it won't be a surprise failure later.
I appreciate the information. We can consider this issue resolved.
@music2myear that's really weird, I've never seen something like that happen before. I'm actually surprised that calling WPF from within the Integrated Console was working before your updates. If you had ever called Out-GridView, Show-Command, or Get-Help -ShowWindow in the session before running your script, any of those would have caused WPF to be loaded. Outside of that I'm entirely unsure.
At any rate, I'm glad to hear that Add-Type worked for you!