PowerShell WPF from XML Findname Problem -


i tested powershell wpf example 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 return $null here. found posts indicating, registername needed, have no idea, how apply here.

as far understand $target window. can try :

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 

------------- edit -------------

here code working findname replace 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  

Comments