gpt4 book ai didi

wpf - 来自 XML Findname 问题的 PowerShell WPF

转载 作者:行者123 更新时间:2023-12-02 23:31:44 28 4
gpt4 key购买 nike

我刚刚测试了来自 here 的早期 PowerShell WPF 示例

#requires -version 2

Add-Type -AssemblyName PresentationFramework

[xml] $xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="408">
<Canvas>
<Button x:Name="button1"
Width="75"
Height="23"
Canvas.Left="118"
Canvas.Top="10"
Content="Click Here" />
</Canvas>
</Window>
"@

$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$target=[Windows.Markup.XamlReader]::Load($reader)

$window= $target.FindName("Window")
$control=$target.FindName("button1")

$eventMethod=$control."add_click"
$eventMethod.Invoke({$window.Title="Hello $((Get-Date).ToString('G'))"})

$target.ShowDialog() | out-null

FindName 似乎在这里返回 $null 。我发现一些帖子表明, 注册名称 是需要的,但我不知道如何在这里应用它。

最佳答案

据我了解,您的 $target 是您的窗口。
你能试一下吗 :

Clear-Host
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
[System.Windows.Window]$Window=[Windows.Markup.XamlReader]::Load($reader)
$Window.Title = "Bonjour"
$controls=$Window.Content
[System.Windows.Controls.Button]$Button = ($controls.Children)[0]
$eventMethod=$Button.add_Click
$eventMethod.Invoke({$window.Title="Hello $((Get-Date).ToString('G'))"})
$Window.ShowDialog() | out-null

- - - - - - - 编辑 - - - - - - -

这是使用 FindName 的代码我替换 Canvas来自 Grid :
#requires -version 2
Add-Type -AssemblyName PresentationFramework
[xml]$xaml =
@"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="408">
<Grid>
<Button x:Name="button1"
Width="75"
Height="23"
Canvas.Left="118"
Canvas.Top="10"
Content="Click Here" />
</Grid>
</Window>
"@

Clear-Host
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$target=[Windows.Markup.XamlReader]::Load($reader)
$control=$target.FindName("button1")
$eventMethod=$control.add_click
$eventMethod.Invoke({$target.Title="Hello $((Get-Date).ToString('G'))"})
$target.ShowDialog() | out-null

关于wpf - 来自 XML Findname 问题的 PowerShell WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5829787/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com