PowerShell で GUI 開発(WPF)とか。
以前、PowerShell で GUI 開発(WinForms)とか。 - sutefu7.comっていうのを書いたのですけど、今度は WPF 版です。各コントロールのサイズや位置調整を考えなくてもいいので超楽です!その代わり、Windowタグ
の名前空間は暗記している方少ないと思いますので、これが問題なんですよね・・・。
test.bat
@echo off powershell -NoProfile -ExecutionPolicy Unrestricted .\test.ps1
test.ps1
Add-Type -AssemblyName PresentationFramework # Visual Studio からコピペした場合は、属性「x:Class="~"」「mc:Ignorable="d"」を削除します。 $xaml = @' <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp2" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <TextBox x:Name="textbox1" Grid.Column="0" AcceptsReturn="True" FontSize="16" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Auto" /> <TextBox x:Name="textbox2" Grid.Column="1" AcceptsReturn="True" FontSize="16" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Auto" /> <Grid Grid.Column="2"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Button x:Name="button1" Grid.Row="0" Content="merge" Height="50" /> <TextBox x:Name="textbox3" Grid.Row="1" AcceptsReturn="True" FontSize="16" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" /> <WebBrowser x:Name="webBrowser1" Grid.Row="2" /> </Grid> </Grid> </Window> '@ $frm = [System.Windows.Markup.XamlReader]::Parse($xaml) $textbox1 = $frm.FindName("textbox1") $textbox2 = $frm.FindName("textbox2") $textbox3 = $frm.FindName("textbox3") $button1 = $frm.FindName("button1") $button1.Add_Click({ $value = "" if (-Not [string]::IsNullOrWhiteSpace($textbox1.Text)) { $value += $textbox1.Text + "`r`n" } if (-Not [string]::IsNullOrWhiteSpace($textbox2.Text)) { $value += $textbox2.Text + "`r`n" } $textbox3.Text = $value }) $frm.ShowDialog()
ところで以前の記事のやつ、WinForms の場合2つ問題があるんですよね(自分の中ではですが)。未確認だけど WPF も同じではないのかな?
1つ目が、各コントロールの Size と Location を脳内でイメージしないといけない問題。職人レベルの方じゃないとなかなか厳しいと思います。だから数字を修正して実行して見て、(以下繰り返し)が必要になります。
2つ目が、Form クラスを継承するとエラー出る現象(悲しい)。
Add-Type -AssemblyName System.Windows.Forms class Form1 : System.Windows.Forms.Form { } $f = New-Object Form1 $f.ShowDialog()
これの継承元クラスが見つかりませんって言われちゃう。
class Base { Hello() { Write-Host "Hello World!" } } class Class1 : Base { } $c = New-Object Class1 $c.Hello()
これだとうまくいくんだけどね~。なんでだろ?
だから、継承してやりたいことができないんです。困った~。