Test.ps1
WPF GUI를 표시하기 위해 다음 PowerShell 스크립트를 작성했습니다 .
function LoadXamlFile( $path )
{
[System.Xml.XmlDocument]$xml = Get-Content -Path $path
$xmlReader = New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $xml
$xaml = [System.Windows.Markup.XamlReader]::Load( $xmlReader )
return $xaml
}
# Main Window
$MainWindow = LoadXamlFile 'MainWindow.xaml'
# Page 1
$Page1 = LoadXamlFile 'Page1.xaml'
$MainWindow.Content = $Page1
$TextBox1 = $MainWindow.FindName('TextBox1')
# The following line fails because $TextBox1 is null
$TextBox1.Text = 'test'
$MainWindow.ShowDialog()
이 스크립트에는 다음 두 개의 XAML 파일이 필요합니다.
MainWindow.xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="MainWindow"
Title="WPF Test" Height="200" Width="400">
</Window>
Page1.xaml
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Page1">
<Grid>
<TextBox x:Name="TextBox1" HorizontalAlignment="Center" Height="23" Margin="0,-40,0,0" TextWrapping="Wrap" VerticalAlignment="Center" Width="120"/>
<Button x:Name="Button1" Content="Next" HorizontalAlignment="Center" Margin="0,40,0,0" VerticalAlignment="Center" Width="76"/>
</Grid>
</UserControl>
내 PowerShell 코드에 명시된 문제는 기본 창에 UserControl을 추가 한 후 UserControl 요소 / 속성에 액세스 할 수 없다는 것입니다. 나는 그것을 사용하여 액세스 할 수 $Page1.FindName('TextBox1')
있지만 $MainWindow
객체 에서 할 수있는 방법이 있습니까?
당신은 할 필요가 FindName
에 Content
의$MainWindow
$TextBox1 = $MainWindow.Content.FindName("TextBox1")
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다