1

I'm looking to be able to get the text from my textbox (folderDialogTextBox.Text), I am able to successfully set the text using the dispatcher, but oddly enough I cannot read the current value of the text.

When running $syncHash from the console, I get:

Name                           Value                                                                                                                                                                                                                             
----                           -----                                                                                                                                                                                                                             
folderDialogTextBox            System.Windows.Controls.TextBox: Please select Browse to search for a directory 

so I can see that I do have some able to see the value "Please select Browse", yet when I run the following command below, I can set the value:

$Global:syncHash.Window.Dispatcher.Invoke([action]{$Global:Synchash.folderDialogTextBox.Text = "Hello"},"Normal")

Yet, when I try to retrieve the value here, I don't get anything back

$Global:syncHash.Window.Dispatcher.Invoke([action]{$Global:Synchash.folderDialogTextBox.Text},"Normal")

Below is the full code with the xml

Thanks for the assistance!

$Global:syncHash = [hashtable]::Synchronized(@{})
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"         
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash)

$psCmd = [PowerShell]::Create().AddScript({   

$inputXML = $xaml = @"
    <Window
    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:Test"
    Title="Test" Height="500" Width="827" WindowStyle="SingleBorderWindow" MinHeight="300" >



<Grid Name="progressBar">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="458*"/>
        <ColumnDefinition Width="292*"/>
        <ColumnDefinition Width="77*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <RichTextBox Name="outputTextBox"   Margin="186,55,10,19" VerticalScrollBarVisibility="Auto" Grid.ColumnSpan="3" IsReadOnly="True" AcceptsTab="True" >
        <FlowDocument>
            <Paragraph>
                <Run Text=""/>
            </Paragraph>
        </FlowDocument>
    </RichTextBox>
    <UniformGrid Background="#FF0291DC" Rows="3" Columns="1" HorizontalAlignment="Left" Width="171">
    </UniformGrid>
    <ProgressBar Height="10" Margin="186,0,10,4" VerticalAlignment="Bottom" IsIndeterminate="True" Grid.ColumnSpan="3"/>
    <TextBox Name="folderDialogTextBox" Margin="186,10,10,0" TextWrapping="Wrap" Text="Please select Browse to search for a directory" VerticalAlignment="Top" Grid.ColumnSpan="2" Height="18"/>
    <CheckBox Name="recursiveButton" Content="Perform Recursive Search (All subfolders)" HorizontalAlignment="Left" Margin="186,33,0,0" VerticalAlignment="Top" Height="15" Width="236"/>
    <Button Name="browseButton" Content="Browse" Margin="0,10,0,0" HorizontalAlignment="Center" Width="57" Grid.Column="2" Height="18" VerticalAlignment="Top"/>




</Grid>


</Window>
"@
[xml]$Global:xmlWPF = $inputXML
#Add WPF and Windows Forms assemblies
try{
Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,system.windows.forms
} catch {
Throw “Failed to load Windows Presentation Framework assemblies.”
}


$Global:syncHash.Window=[Windows.Markup.XamlReader]::Load((new-object System.Xml.XmlNodeReader $xmlWPF))

[xml]$XAML = $xmlWPF

$xmlWPF.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | %{
$Global:synchash.Add($_.Name,$Global:syncHash.Window.FindName($_.Name) )}



$SB = {

$textboxvalue = $Global:syncHash.folderDialogTextBox.Dispatcher.Invoke([action]{$Global:Synchash.folderDialogTextBox.Text},"Normal")
$Global:syncHash.Window.Dispatcher.Invoke([action]{$Global:Synchash.outputTextBox.AppendText("textboxvalue = $textboxvalue`r")},"Normal")

}


$Global:syncHash.browseButton.add_click({Start-Runspace $SB})

$Global:syncHash.Window.ShowDialog() | out-null      
})
$psCmd.Runspace = $newRunspace
$data = $psCmd.BeginInvoke()
start-sleep -Milliseconds 300
Rickybobby
  • 245
  • 1
  • 13

0 Answers0