Livetを使ったC#のWPFプログラミング

システムとして機能が網羅されたサンプルをもとに、Livetを使ったC#のWPFシステムの開発技能を身に付ける為の学習サイト


3.XAML


ViewはXAMLで記述

XAML(Extensible Application Markup Language⇒ザムル)は、WPFに宣言的言語で拡張版XML言語で、画面(View)はXAMLで記述し処理はC#等で記述する方式です。

LivetをインストールしたVisual Studio 2010 Express

新しいプロジェクトは、プロジェクトノ名前を入力して、「Livet WPF4 MVVM アプリケーション」をクリックします。



次にOKをクリックしますと次の画面になります。

全体

通常のWPFアプリケーションの新しいプロジェクトの作成でできる画面と比べますとソリューション エックスプローラーに InfrastructureAssembliesフォルダが作成されており、その中に

  ①Livet.dll

  ②Microsoft.Expression.Interactions.dll

  ③System.Windows.Interactivity.dll

LivetとMicrosoftのライブラリが格納されています。また、参照設定の中を見ますと、この3つのライブラリへの参照が設定されています。


通常のWPFアプリケーションの新しいプロジェクトの作成ではエックスプローラーは次の画面になります。


Livetをインストールした時のViewModel

自動で作成されたViewModelを見ますと

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

using Livet;
using Livet.Commands;
using Livet.Messaging;
using Livet.Messaging.IO;
using Livet.EventListeners;
using Livet.Messaging.Windows;

using LivetWPFApplication1.Models;

namespace LivetWPFApplication1.ViewModels
{
    public class MainWindowViewModel : ViewModel
    {
        public void Initialize()
        {
        }
    }

Livetライブラリの名前空間がusingディレクティブで追加されています。

また、LivetWPFApplication1は、LivetのViewModelを継承すると書かれています。

メソッドとしては、最初に実行されるInitializeが書かれています。


コードビハインド(ファイル)

zamlファイルの下に「~zaml.cs」というファイルが自動でできています。これは、Windowsフォームアプリケーションの時に画面をWクリックして表示されるコードが記述されたファイルと同様な機能のファイルです。

WPFならxaml.cs、Windowsフォームならフォーム.cs、ASP.NETならaspx.csとなります。これらをコードビハインド(ファイル)と呼んでいます。

例えばXAMLの画面でButtonをWクリックしますと、このファイルにClickイベントが書き込まれZAMLのButtonにはClickの記述が追加されます。

View
<Button Content="Button" Height="23" HorizontalAlignment="Left" 
	Margin="395,441,0,0" Name="button1"
        VerticalAlignment="Top" Width="75" Click="button1_Click" />

コードビハインドファイル
private void button1_Click(object sender, RoutedEventArgs e)
{

}

5章 MVVMで解説するように、このサイトでは、LivetというMVVMを使用してWPFのシステムを開発しますので、このコードビハインドは使用しません。

よって、間違ってコントロールをクリックして、このコードが書かれてしまったら、XAMLの3行目のClick="button1_Clickの部分とインドのイコードビハベントプロシージャを削除して下さい。


One Point Lesson 【ZAMLとViewModelの記述のまとめ】
View
■画面遷移の時は「InteractionMessageTrigger 」が必要
         <l:InteractionMessageTrigger
                MessageKey="MessageKey8" Messenger="{Binding Messenger}">
         <l:TransitionInteractionMessageAction
               WindowType="{x:Type v:Window8}" Mode="Modal"/>
         </l:InteractionMessageTrigger>

■TextBox
   <TextBox Text="{Binding Path=txtReceiptNumber, Mode=TwoWay}"
     Height="24" Width="105" VerticalContentAlignment="Center"
     HorizontalAlignment="Left" Margin="114,54,0,0"
     VerticalAlignment="Top" Foreground="White" Background="Black" />
   ※バックがBlackの時は、Foreground="White" Background="Black"を入れる

■処理のコマンドボタン
         <Button Command="{Binding CopyCommand1}" Style="{StaticResource
     CommandTemplate}" Foreground="White"
     Content="CopyCommand" Height="40" HorizontalAlignment="
     Left" Margin="342,65,0,0"
     Name="button16" VerticalAlignment="Top" Width="333" />

ViewModel
■TextBoxの変更通知プロパティ
#region 変更通知プロパティ
public string txtCopy1 { get; set; }
//
private string _txtCopy2;
public string txtCopy2
{
  get { return _txtCopy2; }
  set
  {
    _txtCopy2 = value;
    RaisePropertyChanged("txtCopy2");
  }
}
#endregion

■処理のコマンド
#region CopyCommand1
private ViewModelCommand _copyCommand1;
public ViewModelCommand CopyCommand1
{
  get
  {
    if (this._copyCommand1 == null)
    {
      this._copyCommand1 = new ViewModelCommand(CopyExecute,
      CanCopyExecute);
    }
    return this._copyCommand1;
  }
}
private bool CanCopyExecute()
{
  return true;
}
private void CopyExecute()
{
  txtCopy2 = txtCopy1;
}
#endregion

■画面遷移のコマンド
#region GotoCommand
public ViewModelCommand GotoCommand2
{
  get { return new Livet.Commands.ViewModelCommand(Goto2); }
}
public void Goto2()
{
  Messenger.Raise(new TransitionMessage(new ViewModel2(){ NeedHideOwner =
      true }, "MessageKey2"));
}
#endregion

■閉じるのコマンド
#region CloseCommand
private ViewModelCommand _CloseCommand;
public ViewModelCommand CloseCommand
{
  get
  {
    if (_CloseCommand == null)
    {
      _CloseCommand = new ViewModelCommand(Close);
    }
    return _CloseCommand;
  }
}
public void Close()
{
  var window = Application.Current.Windows.OfType<Window>
  ().SingleOrDefault((w) => w.IsActive);
  window.Close();
}
#endregion