2013年1月5日土曜日

コンテンツ(Window1)


ContentControlクラス    単一エレメントを保持することができるContentと呼ばれるプロパティが含まれている
                                (自分の子供として単一エレメントを保持する)
       クラス定義は、

                Module Name: PresentationFramework.dll

          System.Windows.Controls.ContentControl   ←  これ
            System.Windows.Controls.Control
              System.Windows.FrameworkElement
                System.Windows.UIElement
                  System.Windows.Media.Visual
                    System.Windows.DependencyObject
                      System.Windows.Threading.DispatcherObject
                        System.Object

         単一エレメントを保持する「Content」プロパティは

                >   [System.Windows.Controls.ContentControl]::System.Object get_Content()
                >   [System.Windows.Controls.ContentControl]::Void set_Content(System.Object)
                >   [System.Windows.Controls.ContentControl]::System.Object Content
                >   [System.Windows.Controls.ContentControl]::System.Windows.DependencyProperty ContentProperty

ContentProperty

        「ContentControl」から派生するクラスの一覧は?

        IsPublic IsSerial Name                           BaseType
        -------- -------- ----                                   --------
        True     False    Window                          System.Windows.Controls.ContentControl
        True     False    Label                             System.Windows.Controls.ContentControl
        True     False    ScrollViewer                    System.Windows.Controls.ContentControl
        True     False    ButtonBase                     System.Windows.Controls.ContentControl
        True     False    ListBoxItem                     System.Windows.Controls.ContentControl
        True     False    HeaderedContentControl System.Windows.Controls.ContentControl
        True     False    Frame                            System.Windows.Controls.ContentControl
        True     False    GroupItem                       System.Windows.Controls.ContentControl
        True     False    StatusBarItem                 System.Windows.Controls.ContentControl
        True     False    ToolTip                           System.Windows.Controls.ContentControl
        True     False    UserControl                     System.Windows.Controls.ContentControl


Windowクラスについて調べる

        クラスの定義は以下の通り

            Module Name: PresentationFramework.dll

       System.Windows.Window
         System.Windows.Controls.ContentControl
           System.Windows.Controls.Control
             System.Windows.FrameworkElement
               System.Windows.UIElement
                 System.Windows.Media.Visual
                   System.Windows.DependencyObject
                     System.Windows.Threading.DispatcherObject
                       System.Object

        有益なメソッド
    >   void Show()            Windowを表示する。即処理が戻ってくる
    >   Bool ShowDialog()   Windowを表示する。Closeするまで処理は戻ってこない
    >   Hide()                      Windowを見えなくする

        有益なプロパティ
    >   Topmost                            Bool                                                           最前面に表示
    >   ShowInTaskBar                 Bool                                                           タスクバーにリストする
    >   WindowStartupLocation   "Manual","CenterScreen","CenterOwner" 表示位置
    >   Owner System.Windows.Window                                                  子供Win側で親Winを指定


実際に使ってみる

  1. PowershellだけでWPF表示(Powershell3.0でも2.0でも稼働)
  2. .Netframeworkは3.0以上が必要
  3. WPFの約束としてPowershellはSTAモードで動く必要あり
  4. 以下コードを
      ①Powershellコンソールにコピペして実行
      ②ISEで実行する時はすでにSTAモードなので適時修正して実行
        注意:ブラウザによってはコピペで行頭に余分な空白が出来てしまうことあり
               C:\temp> □'@   ← ここの行の行頭に空白あると動かない 

Windowプロパティの確認
---------------------------------------------
if ($host.Version.Major -eq 3) {
    powershell.exe
} else {
    if ($host.Runspace.ApartmentState -eq "STA") {return}
    powershell.exe -version 2 -sta
}

$xaml_win = @'
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="250" Width="325">

    <DockPanel>
        <Button Content="Windowプロパティの確認" Name="myButton" DockPanel.Dock="Top"/>
        <TextBox Name="myTextBox" FontSize="16" Foreground="Red" MaxLines="15" />
    </DockPanel>

</Window>
'@  # <--ブラウザによっては、コピペ時に行頭に空白出来る。空白削除する必要あり

if ($host.Version.Major -eq 3) {
    add-type -assembly WindowsBase,PresentationCore,PresentationFramework,System.Xml,System.Xaml
} else {
    add-type -assembly WindowsBase,PresentationCore,PresentationFramework,System.Xml
}
$form = [System.Windows.Markup.XamlReader]::Parse($xaml_win)
$form.Topmost = $True
$form.ShowInTaskBar = $True
$form.WindowStartupLocation = @("Manual","CenterScreen","CenterOwner")[1]

[void]$Form.ShowDialog()
exit
--------------------------------------------------------------


Windowの親子関係の例
--------------------------------------------------------------
if ($host.Version.Major -eq 3) {
    powershell.exe
} else {
    if ($host.Runspace.ApartmentState -eq "STA") {return}
    powershell.exe -version 2 -sta
}

$xaml_win = @'
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="150" Width="250">

    <Grid>
        <Button Name="myButton" VerticalAlignment="Top">
            他のWindowを生成
        </Button>
    </Grid>

</Window>
'@  # <--ブラウザによっては、コピペ時に行頭に空白出来る。空白削除する必要あり

$code = @'
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace MyButton
{
    public class EventTest : Window
    {
        public void subscribe(Button b)
        {
            b.Click += Button_Click1;
        }

        public void Button_Click1(object sender, RoutedEventArgs e)
        {
            Window w1 = new Window(); w1.Background = Brushes.AliceBlue;
            w1.Title = "Win 1"; w1.Height = 150; w1.Width = 200;
            w1.Content = "独立Window";
            w1.Show();

            Window w2 = new Window(); w2.Background = Brushes.PaleVioletRed;
            w2.Title = "Win 2"; w2.Height = 150; w2.Width = 200;
            w2.Content = "依存Window(子供)";
            w2.Owner = this; // ★ このWindowを子供指定
            w2.Show();
        }
    }
}
'@  # <--ブラウザによっては、コピペ時に行頭に空白出来る。空白削除する必要あり

if ($host.Version.Major -eq 3) {
    Add-Type $code -ReferencedAssemblies @("WindowsBase","PresentationCore","PresentationFramework","System.Xaml")
} else {
    Add-Type $code -ReferencedAssemblies @("WindowsBase","PresentationCore","PresentationFramework")
}
$form = [System.Windows.Markup.XamlReader]::Parse($xaml_win)
$mb = New-Object MyButton.EventTest
$mb.subscribe($form.FindName("myButton"))
$mb.show()
[void]$Form.ShowDialog()
exit
--------------------------------------------------------------

0 件のコメント:

コメントを投稿