Skip to content

Instantly share code, notes, and snippets.

@posaunehm
Last active December 16, 2015 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save posaunehm/5359587 to your computer and use it in GitHub Desktop.
Save posaunehm/5359587 to your computer and use it in GitHub Desktop.
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 LivetSandbox.Models;
namespace LivetSandbox.ViewModels
{
public class MainWindowViewModel : ViewModel
{
/* コマンド、プロパティの定義にはそれぞれ
*
* lvcom : ViewModelCommand
* lvcomn : ViewModelCommand(CanExecute無)
* llcom : ListenerCommand(パラメータ有のコマンド)
* llcomn : ListenerCommand(パラメータ有のコマンド・CanExecute無)
* lprop : 変更通知プロパティ(.NET4.5ではlpropn)
*
* を使用してください。
*
* Modelが十分にリッチであるならコマンドにこだわる必要はありません。
* View側のコードビハインドを使用しないMVVMパターンの実装を行う場合でも、ViewModelにメソッドを定義し、
* LivetCallMethodActionなどから直接メソッドを呼び出してください。
*
* ViewModelのコマンドを呼び出せるLivetのすべてのビヘイビア・トリガー・アクションは
* 同様に直接ViewModelのメソッドを呼び出し可能です。
*/
/* ViewModelからViewを操作したい場合は、View側のコードビハインド無で処理を行いたい場合は
* Messengerプロパティからメッセージ(各種InteractionMessage)を発信する事を検討してください。
*/
/* Modelからの変更通知などの各種イベントを受け取る場合は、PropertyChangedEventListenerや
* CollectionChangedEventListenerを使うと便利です。各種ListenerはViewModelに定義されている
* CompositeDisposableプロパティ(LivetCompositeDisposable型)に格納しておく事でイベント解放を容易に行えます。
*
* ReactiveExtensionsなどを併用する場合は、ReactiveExtensionsのCompositeDisposableを
* ViewModelのCompositeDisposableプロパティに格納しておくのを推奨します。
*
* LivetのWindowテンプレートではViewのウィンドウが閉じる際にDataContextDisposeActionが動作するようになっており、
* ViewModelのDisposeが呼ばれCompositeDisposableプロパティに格納されたすべてのIDisposable型のインスタンスが解放されます。
*
* ViewModelを使いまわしたい時などは、ViewからDataContextDisposeActionを取り除くか、発動のタイミングをずらす事で対応可能です。
*/
/* UIDispatcherを操作する場合は、DispatcherHelperのメソッドを操作してください。
* UIDispatcher自体はApp.xaml.csでインスタンスを確保してあります。
*
* LivetのViewModelではプロパティ変更通知(RaisePropertyChanged)やDispatcherCollectionを使ったコレクション変更通知は
* 自動的にUIDispatcher上での通知に変換されます。変更通知に際してUIDispatcherを操作する必要はありません。
*/
public void Initialize()
{
}
public void RequestClose(ConfirmationMessage msg)
{
if (msg.Response == true)
{
Messenger.RaiseAsync(new WindowActionMessage("Close", WindowAction.Close));
}
}
}
}
<Window x:Class="LivetSandbox.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:l="http://schemas.livet-mvvm.net/2011/wpf"
xmlns:v="clr-namespace:LivetSandbox.Views"
xmlns:vm="clr-namespace:LivetSandbox.ViewModels"
Title="MainWindow" Height="108.209" Width="247.388">
<Window.DataContext>
<vm:MainWindowViewModel/>
</Window.DataContext>
<i:Interaction.Triggers>
<!--WindowのContentRenderedイベントのタイミングでViewModelのInitializeメソッドが呼ばれます-->
<i:EventTrigger EventName="ContentRendered">
<l:LivetCallMethodAction MethodTarget="{Binding}" MethodName="Initialize"/>
</i:EventTrigger>
<!--Windowが閉じたタイミングでViewModelのDisposeメソッドが呼ばれます-->
<i:EventTrigger EventName="Closed">
<l:DataContextDisposeAction/>
</i:EventTrigger>
<l:InteractionMessageTrigger MessageKey="Close" Messenger="{Binding Messenger}">
<l:WindowInteractionMessageAction />
</l:InteractionMessageTrigger>
<!--WindowのCloseキャンセル処理に対応する場合は、WindowCloseCancelBehaviorの使用を検討してください-->
</i:Interaction.Triggers>
<Grid>
<Button Content="Close" Margin="0,0,10,10" Height="19" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="75">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<l:ConfirmationDialogInteractionMessageAction>
<l:DirectInteractionMessage CallbackMethodName="RequestClose" CallbackMethodTarget="{Binding}">
<l:ConfirmationMessage Image="Question" Caption="Close Confirm" Text="OK?"/>
</l:DirectInteractionMessage>
</l:ConfirmationDialogInteractionMessageAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
</Window>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment