So I am writing a WPF window with powershell using this code
Add-Type -AssemblyName PresentationFramework
class Window
{
[System.Windows.Window]$window
[System.Windows.Controls.Button]$button
[int]$count = 0
Window()
{
[xml]$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window"
Title="C#Shell!">
<Grid x:Name="Grid">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<ListView x:Name="List" />
<Button x:Name="AddButton" Content="Add" Grid.Row="1"/>
</Grid>
</Window>
"@
$reader = [System.Xml.XmlNodeReader]::new($xaml)
$this.window = [System.Windows.Markup.XamlReader]::Load($reader)
$this.button = $this.window.FindName("AddButton")
$this.button.Add_Click({
# How do I access this.window??
$this.window.FindName("List").Items.Add("Item" + ($this.count++).ToString())
})
$this.window.ShowDialog()
}
}
$window = [Window]::new()
How can I access this.window inside that Add_Click script block? This current code gives me
Exception calling "ShowDialog" with "0" argument(s): "You cannot call a method on a null-valued expression."
At line:33 char:9
And I can tell it's not because of ShowDialog(). That line should have been executed, otherwise there will be no window.