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]