2013年1月3日木曜日

レイアウト(Visibility)


3つモードがある

     System.Type : System.Windows.Visibility

    [System.Windows.Visibility]::System.Windows.Visibility Visible        <--ちゃんと計算して可視化
    [System.Windows.Visibility]::System.Windows.Visibility Hidden        <--ちゃんと計算するが不可視
    [System.Windows.Visibility]::System.Windows.Visibility Collapsed    <--レイアウト計算もしない


ちなみに「Visivility」は、どのクラスで定義するかというと(Buttonで)

        Module Name: PresentationFramework.dll

        System.Windows.Controls.Button
           System.Windows.Controls.Primitives.ButtonBase
             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

                    ↓↓↓(確認してみる)

         System.Type : System.Windows.UIElement

        [System.Windows.UIElement]::System.Windows.Visibility get_Visibility()
        [System.Windows.UIElement]::Void set_Visibility(System.Windows.Visibility)
        [System.Windows.UIElement]::System.Windows.Visibility Visibility


実際に試してみる


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


---------------------------------------------

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="125" Width="80">

        <StackPanel>
            <Button Visibility="Visible">Visible</Button>          <!-- ○ちゃんと見えてる -->
            <Button Visibility="Hidden">Hidden</Button>         <!-- ●存在するが見えない -->
            <Button Visibility="Collapsed">Collapsed</Button> <!-- そもそもレイアウトされない -->
            <Button>無指定</Button>                                  <!-- ○最後は、ちゃんと見えてる -->
        </StackPanel>

</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)
[void]$Form.ShowDialog()
exit
--------------------------------------------------------------

0 件のコメント:

コメントを投稿