单击按钮后添加文本或命令

时间:2017-11-07 06:54:32

标签: powershell xaml

在按下按钮后,需要有关在Text_Status中添加文字的建议。 如何在重新启动后自动继续执行命令,让我们说Button_clearTPM,然后转到Button_enableTPM,然后转到Button_initializeTPM

这是我的XAML:

[xml]$xaml = @'
<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TPM Script" Height="482" Width="479" Background="White">
    <Grid Height="375" Width="382">
        <Button Content="Clear TPM" Height="55" HorizontalAlignment="Left" Margin="230,30,0,0" Name="Button_clearTPM" VerticalAlignment="Top" Width="140"/>
        <Button Content="Enable TPM" Height="55" HorizontalAlignment="Left" Margin="230,0,0,220" Name="Button_enableTPM" VerticalAlignment="Bottom" Width="140"/>
        <Button Content="Initialize TPM" Height="55" HorizontalAlignment="Left" Margin="230,169,0,0" Name="Button_initializeTPM" VerticalAlignment="Top" Width="140"/>
        <Label Content="Enter Workstation ID: " Height="23" HorizontalAlignment="Left" Margin="31,45,0,0" Name="Label_1" VerticalAlignment="Top" Width="133"/>
        <TextBox Height="38" HorizontalAlignment="Left" Margin="31,74,0,0" Name="Text_WSID" VerticalAlignment="Top" Width="171" />
        <TextBox Height="114" HorizontalAlignment="Left" Margin="31,241,0,0" Name="Text_Status" VerticalAlignment="Top" Width="329" />
    </Grid>
</Window>
'@

[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
$reader=(New-Object System.Xml.XmlNodeReader $xaml) 

try
{
        $Form=[Windows.Markup.XamlReader]::Load( $reader )
}
catch
{
    Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."t
}
cls


#First Button 
$clearTPM = $Form.FindName('Button_clearTPM')
$clearTPM.Add_Click({ Write-Host "Clear TPM clicked" -ForegroundColor Cyan})

#Second Button 
$enableTPM = $Form.FindName('Button_enableTPM')
$enableTPM.Add_Click({ Write-Host "Enable TPM clicked" -ForegroundColor Cyan})

#Third Button 
$initializeTPM = $Form.FindName('Button_initializeTPM')
$initializeTPM.Add_Click({ Write-Host "Initialize TPM clicked" -ForegroundColor Cyan})

$Form.ShowDialog() | out-null

##Possible Commands
#Clear-Tpm
#Enable-TpmAutoProvisioning (Export C:Notbackedup)
#Initialize-Tpm 

1 个答案:

答案 0 :(得分:0)

问题是你的脚本不知道你的标签是什么。 因此,您必须通过将其变为变量来使其可访问。 然后,您可以通过.Content。

分配您想要的任何文本

所以:

    [xml]$xaml = @'
<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TPM Script" Height="482" Width="479" Background="White">
    <Grid Height="375" Width="382">
        <Button Content="Clear TPM" Height="55" HorizontalAlignment="Left" Margin="230,30,0,0" Name="Button_clearTPM" VerticalAlignment="Top" Width="140"/>
        <Button Content="Enable TPM" Height="55" HorizontalAlignment="Left" Margin="230,0,0,220" Name="Button_enableTPM" VerticalAlignment="Bottom" Width="140"/>
        <Button Content="Initialize TPM" Height="55" HorizontalAlignment="Left" Margin="230,169,0,0" Name="Button_initializeTPM" VerticalAlignment="Top" Width="140"/>
        <Label Content="Enter Workstation ID: " Height="23" HorizontalAlignment="Left" Margin="31,45,0,0" Name="Label_1" VerticalAlignment="Top" Width="133"/>
        <TextBox Height="38" HorizontalAlignment="Left" Margin="31,74,0,0" Name="Text_WSID" VerticalAlignment="Top" Width="171" />
        <TextBox Height="114" HorizontalAlignment="Left" Margin="31,241,0,0" Name="Text_Status" VerticalAlignment="Top" Width="329" />
    </Grid>
</Window>
'@

[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
$reader=(New-Object System.Xml.XmlNodeReader $xaml) 

try
{
        $Form=[Windows.Markup.XamlReader]::Load( $reader )
}
catch
{
    Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."t
}
## THIS IS WHERE MAGIC HAPPENS
    $xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "$($_.Name)" -Value $Form.FindName($_.Name)} # find all names and make them accessible via a variable

#First Button 
$clearTPM = $Form.FindName('Button_clearTPM')
$clearTPM.Add_Click({
    Write-Host "Clear TPM clicked" -ForegroundColor Cyan
       $label_1.content = "Clear TPM clicked"
})

#Second Button 
$enableTPM = $Form.FindName('Button_enableTPM')
$enableTPM.Add_Click({ Write-Host "Enable TPM clicked" -ForegroundColor Cyan})

#Third Button 
$initializeTPM = $Form.FindName('Button_initializeTPM')
$initializeTPM.Add_Click({ Write-Host "Initialize TPM clicked" -ForegroundColor Cyan})

$Form.ShowDialog() | out-null

##Possible Commands
#Clear-Tpm
#Enable-TpmAutoProvisioning (Export C:Notbackedup)
#Initialize-Tpm