Share via

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.

Developer technologies | Visual Studio | Extensions
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

Rich Matheisen 48,116 Reputation points
2023-05-01T21:33:02.7133333+00:00

Add this at the top of your script:

Add-Type -Assembly PresentationFramework

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. MotoX80 37,686 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
    

    Was this answer helpful?

    1 person found this answer helpful.

Your answer

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