ラベル WPFデモ の投稿を表示しています。 すべての投稿を表示
ラベル WPFデモ の投稿を表示しています。 すべての投稿を表示

2013年1月3日木曜日

WPFデモ(Markup拡張)


マークアップ拡張機能とは?

>   <Button Style="{StaticResource SomeStyle}">押下</Button>

    ①マークアップ拡張機能は、XAML以外のクラス(Class名:StaticResource)をフック
    ②マークアップ拡張機能は、そのコントローラの属性値として利用される
    ③{}かっこ内の右辺は、このクラスのコンストラクタ・パラメータ
        例1
        >   <Button Content="{local:ShowTime First}"></Button>
                String値Firstをコンストラクタパラメータとしてクラスを生成
        例2
        >   <Button Content="{local:ShowTime Header=Second}"></Button>
                空のコンストラクタでクラスを生成し、プロパティHearderにString値Secondをセット

    ④このクラスは拡張クラスとして機能するように作られている必要あり(「MarkupExtention」クラスを拡張)
        >   public override object ProvideValue(IServiceProvider serviceProvider)
        >   {
        >       return string.Format("{0}: {1}", header, DateTime.Now.ToLongTimeString());
        >   }

WPFデモ(Binding2)

  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="Markup Extensions" Height="220" Width="175">

<StackPanel>
<Label Name="displayText" Margin="5"/>
<TextBox Name="sourceInfo">10</TextBox>
</StackPanel>

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

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

namespace MyBind
{
public class BindingClass
{
public void bindData(object source, object dest)
{
TextBox s1 = (TextBox)source;
Label   d1 = (Label)dest;

//Bindingの生成
Binding bindingShared = new Binding();

// SourceとPathの設定
bindingShared.Source = s1;

bindingShared.Path = new PropertyPath( "Text" );

//Bindingオブジェクトのセット
d1.SetBinding( Label.ContentProperty, bindingShared );
d1.SetBinding( Label.FontSizeProperty, bindingShared );
}
}
}
'@  # <--ブラウザによっては、コピペ時に行頭に空白出来る。空白削除する必要あり

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)
$textBox = $form.FindName("sourceInfo")
$label = $form.FindName("displayText")
$mb = new-object MyBind.BindingClass
$mb.bindData($textBox, $label);
[void]$Form.ShowDialog()
exit
------------------------------------------------

WPFデモ(Binding)

  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
          }


          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
          }

          $Xaml_win = @'
          <Window
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              Title="Markup Extensions" Height="220" Width="175">

          <StackPanel>
             <TextBox Name="tbValue" Margin="10"
          Text="{Binding ElementName=sldrSlider, Path=Value, Mode=TwoWay}"/>
             <Slider Name="sldrSlider" TickPlacement="TopLeft" Margin="10"/>
          <Button Name="bnValue">Trigger</Button>
          </StackPanel>

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

          $form = [System.Windows.Markup.XamlReader]::Parse($xaml_win)

          $script = {
          $bindExpression = $form.FindName("tbValue").GetBindingExpression(`
           [System.Windows.Controls.TextBox]::TextProperty)
          $bindExpression.UpdateSource()
          }
          $form.findName("bnValue").add_Click($script)
          [void]$Form.ShowDialog()
          exit
          -----------------------------------------

          WPFデモ(Hello,World)

          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="170" Width="350">

                  <Button Name="mybutton" FontSize="18" FontWeight="Bold">
                      二階堂 to WPF with PowerShell
                  </Button>

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

          $code = @'
          using System;
          using System.Windows;
          using System.Windows.Controls;
          using System.Windows.Media;
          namespace MyButton
          {
              public class EventTest
              {
                  public void subscribe(Button b)
                  {
                      //b.Click += Button_Click1;
                      b.Click += (RoutedEventHandler)Delegate.CreateDelegate(typeof(RoutedEventHandler), this, "Button_Click1");
                  }
                  public void Button_Click1(object sender, RoutedEventArgs e)
                  {
                      Console.WriteLine((Button)sender);
                      Console.WriteLine(e);
                      MessageBox.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"))
          [void]$Form.ShowDialog()

          exit

          #-----------------------------------------------------------------