Add this at the top of your script:
Add-Type -Assembly PresentationFramework
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I try a simple example from
https://learn-powershell.net/2014/08/10/powershell-and-wpf-radio-button/
[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"
SizeToContent = "WidthAndHeight" ShowInTaskbar = "True" Background = "lightgray">
<StackPanel >
<RadioButton x:Name="Item1" Content = 'Item1'/>
<RadioButton x:Name="Item2" Content = 'Item2'/>
<RadioButton x:Name="Item3" Content = 'Item3'/>
<TextBox />
</StackPanel>
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load( $reader )
$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | ForEach
{
Set-Variable -Name ($_.Name) -Value $Window.FindName($_.Name)
}
$Window.Showdialog() | Out-Null
I get error:
[ERROR] Unable to find type [Windows.Markup.XamlReader].
[ERROR] At line:17 char:9
[ERROR] + $Window=[Windows.Markup.XamlReader]::Load( $reader )
[ERROR] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~
[ERROR] + CategoryInfo : InvalidOperation: (Windows.Markup.XamlReader:Typ
[ERROR] eName) [], RuntimeException
[ERROR] + FullyQualifiedErrorId : TypeNotFound
[ERROR]
cmdlet ForEach-Object at command pipeline position 1
Supply values for the following parameters:
Process: Name Bill
[ERROR] ForEach-Object : Cannot bind parameter 'Process'. Cannot convert the "Name
[ERROR] Bill" value of type "System.String" to type
[ERROR] "System.Management.Automation.ScriptBlock".
[ERROR] At line:20 char:77
[ERROR] + ... des("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | ForEach
[ERROR] + ~~~~~~~
[ERROR] + CategoryInfo : InvalidArgument: (:) [ForEach-Object], Parameter
[ERROR] BindingException
[ERROR] + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerSh
[ERROR] ell.Commands.ForEachObjectCommand
[ERROR]
Set-Variable -Name ($_.Name) -Value $Window.FindName($_.Name)
[ERROR] You cannot call a method on a null-valued expression.
[ERROR] At line:25 char:1
[ERROR] + $Window.Showdialog() | Out-Null
[ERROR] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[ERROR] + CategoryInfo : InvalidOperation: (:) [], RuntimeException
[ERROR] + FullyQualifiedErrorId : InvokeMethodOnNull
[ERROR]
Please advise as to what went wrong.
Since the example is older than vs2019, that shouldn't be the issue.
I have PowerShell extensions installed.
Add this at the top of your script:
Add-Type -Assembly PresentationFramework
I can't explain why it's prompting for a process, but I was able to get it to work this way.
Add-Type -Assembly PresentationFramework
[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"
SizeToContent = "WidthAndHeight" ShowInTaskbar = "True" Background = "lightgray">
<StackPanel >
<RadioButton x:Name="Item1" Content = 'Item1'/>
<RadioButton x:Name="Item2" Content = 'Item2'/>
<RadioButton x:Name="Item3" Content = 'Item3'/>
<TextBox />
</StackPanel>
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load( $reader )
$zzz = $xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]")
ForEach ($z in $zzz)
{
Set-Variable -Name ($z.Name) -Value $Window.FindName($z.Name)
}
$Window.Showdialog() | Out-Null