Simple Powershell with Vs2019 radio buttons question

Boyd Mills 21 Reputation points
2023-05-01T20:37:41.5433333+00:00

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.

Visual Studio Extensions
Visual Studio Extensions
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Extensions: A program or program module that adds functionality to or extends the effectiveness of a program.
214 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,505 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 46,711 Reputation points
    2023-05-01T21:33:02.7133333+00:00

    Add this at the top of your script:

    Add-Type -Assembly PresentationFramework
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. MotoX80 33,481 Reputation points
    2023-05-01T21:32:42.29+00:00

    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
    
    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.