无法在Powershell中获取文本框对象的内容

时间:2019-10-08 14:08:34

标签: powershell textbox

所以我花了很多时间寻找解决方案的提示,但基本上什么也没找到。 我正在尝试使用Powershell构建GUI,该GUI在文本框中接受用户输入,并且我试图使在按“输入”键时获取该文本框的内容成为可能。 到目前为止,一切都很好,确实可以工作,但是我不知道输出是哪种变量。当然不是字符串。

if($WPFtextBox.Key -eq 'Enter')
{  

$Whyisntitworking = $WPFtextBox.Text
$WPFTextBlock.Text = $Whyisntitworking
    }

就像我说的,我只是想了解一下,所以它实际上并没有做任何事情。 如果需要整个代码或可以帮助您,请告诉我。 我知道KeyDown-Event可以正常工作,而且一切都好,我只是无法在光标位于文本框中并按Enter的情况下将Text从文本框中移出。

编辑:

    $inputXML = @"
<Window x:Class="WpfApplication1.MainWindow"
        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:WpfApplication1"
        mc:Ignorable="d"
        Title="Eingabefenster" Height="350" Width="525">
    <Grid>
        <Image x:Name="image" HorizontalAlignment="Left" Height="122" Margin="15,15,0,0" VerticalAlignment="Top" Width="160" Source="C:\Users\hoesl\Pictures\Saved Pictures\Kawkaw.jpg"/>
        <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Height="90" Margin="207,15,0,0" TextWrapping="Wrap" Text="&lt;- This is judgemental parrot. He's judging you!" VerticalAlignment="Top" Width="280" Background="Black" Foreground="White" FontFamily="Segoe WP Black"/>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="14" Margin="367,123,0,0" TextWrapping="Wrap" Text="Type here..." VerticalAlignment="Top" Width="120" FontSize="9.333" Foreground="#FF9C9C9C"/>
        <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="16" Margin="367,170,0,0" TextWrapping="Wrap" Text="Type here..." VerticalAlignment="Top" Width="120" FontSize="9.333" Foreground="#FF9C9C9C"/>
        <TextBlock x:Name="textBlock1" HorizontalAlignment="Left" Margin="207,123,0,0" TextWrapping="Wrap" Text="Attribute&#x9;" VerticalAlignment="Top" Height="23" Width="72"/>
        <TextBlock x:Name="textBlock2" HorizontalAlignment="Left" Margin="207,170,0,0" TextWrapping="Wrap" Text="Identity" VerticalAlignment="Top" Height="23" Width="72"/>
        <Button x:Name="button" Content="Confirm" HorizontalAlignment="Left" Margin="367,220,0,0" VerticalAlignment="Top" Width="120" BorderBrush="#FF787878" Background="#FFE4E4E4"/>

    </Grid>
</Window>

"@ 

$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = $inputXML
#Read XAML

$reader=(New-Object System.Xml.XmlNodeReader $xaml)
try{
    $Form=[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
}

#===========================================================================
# Load XAML Objects In PowerShell
#===========================================================================

$xaml.SelectNodes("//*[@Name]") | %{"trying item $($_.Name)";
    try {Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name) -ErrorAction Stop}
    catch{throw}
    }

Function Get-FormVariables{
if ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true}
write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
get-variable WPF*
}

Get-FormVariables

#===========================================================================
# Use this space to add code to the various form elements in your GUI
#===========================================================================
$WPFbutton.Add_Click(
{
    If(($WPFtextBox.Text -eq 'a')){
        $WPFtextBox1.Text = $env:COMPUTERNAME
        $WPFTextBlock.Text = "Test Success!"
    } Else {
        $WPFtextBox1.Text = "Congratulations, you didn't break it"
    }

}) 




$WPFtextBox.Add_KeyDown{ 
param
(
  [Parameter(Mandatory)][Object]$sender,
  [Parameter(Mandatory)][Windows.Input.KeyEventArgs]$WPFtextBox
)



if($WPFtextBox.Key -eq 'a')
{
    $WPFtextBox1.Text = ""
    }


if($WPFtextBox.Key -eq 'Enter')
    {  

    $Whyisntitworking = $WPFtextBox.Text
    $WPFTextBlock.Text = $Whyisntitworking

$WPFTextBlock.Text = "At least the key recognition is working"
    }



}









$Form.ShowDialog()

同样是关于最后一个if子句。 如果复制粘贴后它不起作用,我可能忘了将每个}放在这里后再添加它,不得不对其进行编辑以进行清理以免显得笨拙。

0 个答案:

没有答案