-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
refreshingBrowserViewModel.ps1
74 lines (54 loc) · 2.01 KB
/
refreshingBrowserViewModel.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
[string]$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="Initial Window" Width="800" Height="600">
<Grid>
<TextBox FontSize="24" Text="{Binding Text}" Grid.ColumnSpan="3" TextWrapping="Wrap" />
<ProgressBar Value="{Binding Progress}" Grid.ColumnSpan="3" Grid.Row="1" />
<Button Command="{Binding AddStar}" Content="Add *" Grid.Row="2" Grid.Column ="1" />
<Button Command="{Binding RunBackgroundTask}" Content="Run background task" Grid.Row="2" />
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
</Window>
"@
$Window=[Windows.Markup.XamlReader]::Parse($xaml)
$Window.DataContext = [MainViewModel]::new()
$Window.ShowDialog()
class MainViewModel : WpfToolkit.ViewModelBase {
[String] $Text = "*"
[int] $Progress
[Windows.Input.ICommand] $RunBackgroundTask
[Windows.Input.ICommand] $AddStar
MainViewModel () {
Write-Host "Constructing Main view Model"
$this.Init('Text')
$this.Init('Progress')
$work = {
param($this, $o)
Dispatch { $this.SetProgress(10) }
Start-Sleep -Seconds 2
Dispatch { $this.SetProgress(50) }
Start-Sleep -Seconds 2
Dispatch { $this.SetProgress(90) }
}
$callback = {
param($this)
$this.SetText($this.Text + " Background task done. ")
$this.SetProgress(100)
}
$this.RunBackgroundTask = $this.NewBackgroundCommand($work, $callback)
$this.AddStar = $this.NewCommand({
Write-Host "is `$this null?: $($null -eq $this)"
$this.SetText($this.Text + "*")
})
}
}