【发布时间】:2019-11-04 21:45:39
【问题描述】:
我正在使用 XAML 构建一个带有 GUI 的 PowerShell 脚本。我正在调用一个函数(形成一个 DLL 文件),它正在控制台中写入其输出,但我想让它改为在 TextBox 中写入。请参阅下图,了解当我单击按钮 CHECK DATA 时会发生什么,该按钮正在调用我为测试而创建的虚拟方法 set_Value。
我尝试使用redirections,但没有成功。我只能让它写入文件。我确定我遗漏了什么,但不确定是什么。
XAML
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DataBase Export Import Tool" Height="469.28" Width="760" WindowStartupLocation="CenterScreen" ResizeMode='NoResize'>
<Grid Margin="0,0,-0.2,0.2">
<Label Content="From Client" HorizontalAlignment="Left" Margin="19,10,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#006271" Foreground="White"/>
<Label Content="To Client" HorizontalAlignment="Left" Margin="390,10,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#006271" Foreground="White"/>
<Label Content="From Environment" HorizontalAlignment="Left" Margin="19,45,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#006271" Foreground="White"/>
<Label Content="To Environment" HorizontalAlignment="Left" Margin="390,45,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#006271" Foreground="White"/>
<Label Content="SQL Server" HorizontalAlignment="Left" Margin="19,80,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#006271" Foreground="White"/>
<Label Name="prdSqlResult" Content="Server" HorizontalAlignment="Left" Margin="194,80,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="White" Foreground="#006271"/>
<Label Content="SQL Server" HorizontalAlignment="Left" Margin="390,80,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#006271" Foreground="White"/>
<Label Name="sqlResult" Content="Server" HorizontalAlignment="Left" Margin="565,80,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="White" Foreground="#006271"/>
<Label Content="Database" HorizontalAlignment="Left" Margin="19,115,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#006271" Foreground="White"/>
<Label Name="prdDbResult" Content="Database" HorizontalAlignment="Left" Margin="194,115,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="White" Foreground="#006271"/>
<Label Content="Database" HorizontalAlignment="Left" Margin="390,115,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#006271" Foreground="White"/>
<Label Name="dbResult" Content="Database" HorizontalAlignment="Left" Margin="565,115,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="White" Foreground="#006271"/>
<TextBox Name="OutputBox" Height="200" Width="720" TextWrapping="Wrap" AcceptsReturn="True" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto" Margin="0,100,0,0" />
<Button Name="checkData" Content="Check Data" HorizontalAlignment="Left" Margin="19,387,0,0" Width="220" BorderThickness="0" RenderTransformOrigin="0.496,-2.677" Height="30" VerticalAlignment="Top"/>
<Button Name="btndbe" Content="Run Database Export" HorizontalAlignment="Left" Margin="268,387,0,0" Width="220" BorderThickness="0" RenderTransformOrigin="0.496,-2.677" Height="30" VerticalAlignment="Top"/>
<Button Name="btndbi" Content="Run Database Import" HorizontalAlignment="Left" Margin="516,387,0,0" Width="220" BorderThickness="0" RenderTransformOrigin="0.496,-2.677" Height="30" VerticalAlignment="Top"/>
<StackPanel Margin="0">
<ComboBox Name="comboFromClient" IsEditable="False" HorizontalAlignment="Left" Height="25" Margin="194,10,0,0" Width="170">
<ComboBoxItem>1</ComboBoxItem>
<ComboBoxItem>2</ComboBoxItem>
<ComboBoxItem>3</ComboBoxItem>
</ComboBox>
</StackPanel>
<StackPanel Margin="0">
<ComboBox Name="comboFromEnvironment" IsEditable="False" HorizontalAlignment="Left" Height="25" Margin="194,45,0,0" Width="170">
<ComboBoxItem ToolTip="This it tool tip for item 1">PRD</ComboBoxItem>
<ComboBoxItem>A</ComboBoxItem>
<ComboBoxItem>B</ComboBoxItem>
<ComboBoxItem>C</ComboBoxItem>
</ComboBox>
</StackPanel>
<StackPanel Margin="0">
<ComboBox Name="comboToClient" IsEditable="False" HorizontalAlignment="Left" Height="25" Margin="565,10,0,0" Width="170">
<ComboBoxItem>1</ComboBoxItem>
<ComboBoxItem>2</ComboBoxItem>
<ComboBoxItem>3</ComboBoxItem>
<ComboBoxItem>CN</ComboBoxItem>
</ComboBox>
</StackPanel>
<StackPanel Margin="0">
<ComboBox Name="comboToEnvironment" IsEditable="False" HorizontalAlignment="Left" Height="25" Margin="565,45,0,0" Width="170">
<ComboBoxItem>A</ComboBoxItem>
<ComboBoxItem>B</ComboBoxItem>
</ComboBox>
</StackPanel>
</Grid>
</Window>
PS1
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null
Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,system.windows.forms
$XAML = [xml](Get-Content -Path "$PSSCriptRoot\GuiDataImportExport.xaml")
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
#Dummy function to be able to get some output instead using the real method
function set-Value{
for($i = 0; $i -lt 5; $i++){
$sortie = $i | timestamp
Write-Host $sortie
Sleep -Seconds 1
}
}
function timestamp { Process{"$(Get-Date): $_"} }
$Form=[Windows.Markup.XamlReader]::Load( $reader )
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}
$OutputBox = $Form.FindName("OutputBox")
$checkData.Add_Click({
#this writes in the console
set-Value
#This writes in a file
$OutputBox.AppendText((&{set-Value} *>> "$PSScriptRoot\log.log"))
})
$Form.ShowDialog() | out-null
如果你们需要更多详细信息,请告诉我。
【问题讨论】:
标签: powershell xaml redirect textbox