XML Findname 中的 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 。我发现一些帖子表明需要 RegisterName ,但我不知道如何在这里应用它。
I just tested an early PowerShell WPF example from here
#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 seems to return $null here. I found some posts indicating, that RegisterName is needed, but I have no idea, how to apply this here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我了解,您的 $target 是您的窗口。
你可以尝试吗:
------------- 编辑 -------------
这是与
FindName
一起使用的代码,我替换由
:Grid
绘制的画布As far as I understand your $target is your window.
Can you try :
------------- EDIT -------------
Here is the code working with
FindName
I replaceCanvas
byGrid
:只需给你的窗口一个“x:Name”:
Just give a "x:Name" to your window: