Skip to content

Instantly share code, notes, and snippets.

@kokudori
Created February 9, 2015 12:42
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 kokudori/1d2b6db8ba68cfed612a to your computer and use it in GitHub Desktop.
Save kokudori/1d2b6db8ba68cfed612a to your computer and use it in GitHub Desktop.
ClockApp
<Window x:Class="LivetWPFApplication1.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:LivetWPFApplication1.Views"
xmlns:vm="clr-namespace:LivetWPFApplication1.ViewModels"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<vm:MainWindowViewModel/>
</Window.DataContext>
<i:Interaction.Triggers>
<!--Viewに特別な要件が存在しない限りは、トリガーやアクションの自作にこだわらず積極的にコードビハインドを使いましょう -->
<!--Viewのコードビハインドは、基本的にView内で完結するロジックとViewModelからのイベントの受信(専用リスナを使用する)に限るとトラブルが少なくなります -->
<!--Livet1.1からはコードビハインドでViewModelのイベントを受信するためのWeakEventLisnterサポートが追加されています -->
<!--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>
<!--WindowのCloseキャンセル処理に対応する場合は、WindowCloseCancelBehaviorの使用を検討してください-->
</i:Interaction.Triggers>
<Grid>
<TextBlock HorizontalAlignment="Left" Margin="225,149,0,0" TextWrapping="Wrap" Text="{Binding Clock}" VerticalAlignment="Top"/>
</Grid>
</Window>
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;
using System.Threading.Tasks;
namespace LivetWPFApplication1.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を操作する必要はありません。
*/
#region Clock変更通知プロパティ
private string _Clock;
public string Clock
{
get
{ return _Clock; }
set
{
if (_Clock == value)
return;
_Clock = value;
RaisePropertyChanged();
}
}
#endregion
public void Initialize()
{
Loop();
}
async Task Loop()
{
while (true)
{
Clock = DateTime.Now.ToString();
await Task.Delay(1000);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment