WPF PowerShell GUI Function Return values in wrong order when using xaml ?

Borut Puhar 66 Reputation points
2022-11-23T11:55:41.303+00:00

I have create a function and I use WPF and GUI.
I call this function and I need return values from function…
To get correct return I need to caal las valuses (-1, -2,..)

In function im returning : return $Var_S1_Text, $BBB, $($var_TexB_2.Text);
$A return array return following values...
trying item Button_1
trying item TexB_1
trying item TexB_2
False
Alfa
2 <- $A[-2]
Some new text <- $A[-1]

Is this normal or I need to modify script… because I need only last three values ?
Sample text :
unction ClickMe {

    Param  
    (  
    [Parameter(Mandatory = $true)] $BBB  
    )  
  
$inputXML = @"  
<Window x:Name="Winodw1" x:Class="WindowApp0.Window3"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
        xmlns:local="clr-namespace:WindowApp0"  
        mc:Ignorable="d"  
        Title="My First Window" Height="450" Width="800">  
    <Grid>  
        <Button x:Name="Button_1" Content="Button" HorizontalAlignment="Left" Margin="19,329,0,0" VerticalAlignment="Top" Height="50" Width="124" Grid.Column="1"/>  
        <TextBlock x:Name="TexB_1" HorizontalAlignment="Left" Margin="19,144,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="114" Height="32" Grid.Column="1"/>  
        <TextBlock x:Name="TexB_2" HorizontalAlignment="Left" Margin="170,144,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="174" Height="26" Grid.Column="1" Text="Some text"/>  
  
    </Grid>  
</Window>  
  
"@   
  
$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window' -replace 'TextChanged="TextBox(.*)','/>"'  
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')  
[xml]$XAML = $inputXML  
  
$reader=(New-Object System.Xml.XmlNodeReader $xaml)  
try{  
    $FormA=[Windows.Markup.XamlReader]::Load( $reader )  
}  
catch{  
    Write-Warning "Unable to parse XML, with error: $($Error[0])`n Ensure that there are NO SelectionChanged or TextChanged properties in your textboxes (PowerShell cannot process them)"  
    throw  
}  
  
$xaml.SelectNodes("//*[@Name]") | %{"trying item $($_.Name)";  
    try {Set-Variable -Name "var_$($_.Name)" -Value $FormA.FindName($_.Name) -ErrorAction Stop}  
    catch{throw}  
    }  
  
$var_TexB_2.Text = "Some new text"  
Write-host "VAR is = $Var_S1_Text and $BBB" -ForegroundColor Green   
    [string]$Var_S1_Text = "Alfa"  
    [string]$BBB = "2"  
    $Var_Button_1.IsCancel = $true  
  
    $Var_Button_1.Add_Click({  
      $FormA.Close()})  
    $FormA.ShowDialog()   
    return $Var_S1_Text, $BBB, $($var_TexB_2.Text);  
  
}  
  
$B = "1"  
$A = @()  
$A = ClickMe $B  
Write-Host "$($A[0]) and $($A[1]) " -ForegroundColor Cyan  
$A[-1]  
$A[-2]  
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,449 questions
{count} vote

4 answers

Sort by: Most helpful
  1. Rich Matheisen 45,671 Reputation points
    2022-11-24T04:13:49.773+00:00

    On line #45 you reference variable $Var_S1_Text before it's defined on line #46. Is that what you intended?

    On line #39 you put the value "trying item $($_.Name)" into the Success stream. I think you meant to use Write-Host "trying item $($_.Name)" which would only show the string on the console.

    On line #52 you place the value of the button that was clicked into the Success stream instead of assigning it to a variable.

    Try this:

    function ClickMe {  
        Param  
        (  
            [Parameter(Mandatory = $true)] $BBB  
        )  
      
        $inputXML = @"  
    <Window x:Name="Winodw1" x:Class="WindowApp0.Window3"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
        xmlns:local="clr-namespace:WindowApp0"  
        mc:Ignorable="d"  
        Title="My First Window" Height="450" Width="800">  
    <Grid>  
        <Button x:Name="Button_1" Content="Button" HorizontalAlignment="Left" Margin="19,329,0,0" VerticalAlignment="Top" Height="50" Width="124" Grid.Column="1"/>  
        <TextBlock x:Name="TexB_1" HorizontalAlignment="Left" Margin="19,144,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="114" Height="32" Grid.Column="1"/>  
        <TextBlock x:Name="TexB_2" HorizontalAlignment="Left" Margin="170,144,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="174" Height="26" Grid.Column="1" Text="Some text"/>  
      
    </Grid>  
    </Window>  
      
    "@   
      
        $inputXML = $inputXML -replace 'mc:Ignorable="d"', '' -replace "x:N", 'N' -replace '^<Win.*', '<Window' -replace 'TextChanged="TextBox(.*)', '/>"'  
        [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')  
        [xml]$XAML = $inputXML  
      
        $reader = (New-Object System.Xml.XmlNodeReader $xaml)  
        try {  
            $FormA = [Windows.Markup.XamlReader]::Load( $reader )  
        }  
        catch {  
            Write-Warning "Unable to parse XML, with error: $($Error[0])`n Ensure that there are NO SelectionChanged or TextChanged properties in your textboxes (PowerShell cannot process them)"  
            throw  
        }  
      
        $xaml.SelectNodes("//*[@Name]") | % { Write-Host "trying item $($_.Name)" -ForegroundColor Yellow;  
            try { Set-Variable -Name "var_$($_.Name)" -Value $FormA.FindName($_.Name) -ErrorAction Stop }  
            catch { throw }  
        }  
      
        $var_TexB_2.Text = "Some new text"  
        Write-Host "VAR is = $Var_S1_Text and $BBB" -ForegroundColor Green   
        [string]$Var_S1_Text = "Alfa"  
        [string]$BBB = "2"  
        $Var_Button_1.IsCancel = $true  
      
        $Var_Button_1.Add_Click({  
                $FormA.Close() })  
        $result = $FormA.ShowDialog()   
        return $Var_S1_Text, $BBB, $($var_TexB_2.Text);  
      
    }  
      
    $B = "1"  
    $A = @()  
    $A = ClickMe $B  
    Write-Host "$($A[0]) and $($A[1]) " -ForegroundColor Cyan  
    $A[-1]  
    $A[-2]  
    
    0 comments No comments

  2. Borut Puhar 66 Reputation points
    2022-11-24T09:01:27.063+00:00
         function ClickMe {  
             Param  
             (  
                 [Parameter(Mandatory = $true)] $BBB  
             )  
              
    $inputXML = @"  
    <Window x:Class="WindowApp0.Window1"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WindowApp0"  
            mc:Ignorable="d"  
            Title="TEST" Height="500" Width="500" Topmost="True" WindowStartupLocation="CenterScreen">  
         <Grid>  
             <Button x:Name="Button_1" Content="Button" HorizontalAlignment="Left" Margin="19,329,0,0" VerticalAlignment="Top" Height="50" Width="200"/>  
             <TextBlock x:Name="TexB_1" HorizontalAlignment="Left" Margin="19,144,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="114" Height="32" Text="Some text"/>  
             <TextBlock x:Name="TexB_2" HorizontalAlignment="Left" Margin="170,144,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="174" Height="26" Text="Some text"/>   
         </Grid>  
    </Window>     
    "@   
              
             $inputXML = $inputXML -replace 'mc:Ignorable="d"', '' -replace "x:N", 'N' -replace '^<Win.*', '<Window' -replace 'TextChanged="TextBox(.*)', '/>"'  
             [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')  
             [xml]$XAML = $inputXML  
              
             $reader = (New-Object System.Xml.XmlNodeReader $xaml)  
             try {  
                 $FormA = [Windows.Markup.XamlReader]::Load( $reader )  
             }  
             catch {  
                 Write-Warning "Unable to parse XML, with error: $($Error[0])`n Ensure that there are NO SelectionChanged or TextChanged properties in your textboxes (PowerShell cannot process them)"  
                 throw  
             }  
              
             $xaml.SelectNodes("//*[@Name]") | % { Write-Host "trying item $($_.Name)" -ForegroundColor Yellow;  
                 try { Set-Variable -Name "var_$($_.Name)" -Value $FormA.FindName($_.Name) -ErrorAction Stop }  
                 catch { throw }  
             }  
              
             $var_TexB_2.Text = "Some new text"  
             [string]$Var_S1_Text = "Alfa"  
             [string]$BBB = "2"  
             $Var_Button_1.IsCancel = $true  
       
             $Var_Button_1.Add_Click({  
                     $FormA.Close() })  
       
             ##################################################  
             $result = $FormA.ShowDialog()  
             return $Var_S1_Text, $BBB, $($var_TexB_2.Text);  
      
       
         }  
              
         $B = "1"  
         $A = @()  
         $A = ClickMe $B  
         $A[-1]  
         $A[-2]  
    
    0 comments No comments

  3. Borut Puhar 66 Reputation points
    2022-11-24T09:06:18.093+00:00

    If I change lines 51 and 52, so thet line
    “return $Var_S1_Text, $BBB, $($var_TexB_2.Text);”
    “$result = $FormA.ShowDialog()”

    Then From is NOT shown.

    Issue is what is return in $A.
    I expect return for three variable, but I also get return for
    trying item Button_1
    trying item TexB_1
    trying item TexB_2

    Alfa and 2
    Some new text
    2

    Expected return should be :
    Alfa and 2
    Some new text
    2
    PS: yes this is sample, something to shown the issue..


  4. Borut Puhar 66 Reputation points
    2022-11-24T13:08:37.187+00:00
    Ok,   
    I solve one part : https://riptutorial.com/powershell/example/27037/how-to-work-with-functions-returns  
    Now question is :  
    I have Function 1   when I click button  it calls Function 2 and then return Values from Function 2  
    How to close Function1 to return values to original script. One option is global variables but ?  
    Im missing something or it something in front of my nose…  
    Function2 ***  
    {  
    *  
    .{  
    *  
    *  
    *  
      
    $Var_L1_But0.Add_Click({  
        $var_L1_tex2.Text = $Par1_L1 + $var_L0_tex2.Text  
        # $Form1.Close()  
        })  
    $Form1.ShowDialog()   | out-null  
    } | Out-Null  
      
    return $($var_L1_tex2.Text) # I can se value  
    }  
      
      
    Function1 ****  
    {  
    ***  
    .{  
    *  
    *  
    *  
    $Var_L0_But0.Add_Click({  
        $var_L0_tex2.Text = $Par0_L0 + $var_L0_tex2.Text   
        $Sum0 = Function2 1 $var_L0_tex2.Text  
        Write-Host "Varible F0 : $Sum0"  # I can se value  
      
         # return  
        }) | Out-Null  
      
    $Form0.ShowDialog()   
      
    } | Out-Null  
      
       Write-Host "Varible F0 : $Sum0 and $Sum4"  
       return $Sum0  
      
    }  
      
    $Sum =Function1 1  
      
    $Sum  
    
    0 comments No comments