かずきのBlog@hatena

すきな言語は C# + XAML の組み合わせ。Azure Functions も好き。最近は Go 言語勉強中。日本マイクロソフトで働いていますが、ここに書いていることは個人的なメモなので会社の公式見解ではありません。

ReactivePropertyのPCL版をnugetに置いてみました(まだベータ)

のいえさん(id:neuecc)が作った素敵なReactivePropertyをPCL対応にしてみました。CodePlexのプロジェクトのメンバーにじんぐるさんと共に追加してもらって、とりあえず動く感じまでやってみました。

ターゲットプラットフォームは以下のものをサポートしています。

  • .NET Framework 4.5
  • Windows store app(Windows 8.1)
  • Windows Phone 8
  • Xamarin iOS(試して無いけどできそう)
  • Xamarin Android(試して無いけどできそう)

インストール方法

Visual Studioのパッケージマネージャー コンソールで以下のコマンドでインストールできます。

PM> Install-Package ReactiveProperty-PCL -Pre

コードスニペット

packages\ReactiveProperty-PCL.0.4.0.1-beta\Snippetにコードスニペットが入ってるので追加しておくと捗ると思います。

お試し

WPFでこんな感じでDataContextにセットするViewModelが書けるのが気持ちいい。

using Codeplex.Reactive;
using System;
using System.Reactive.Linq;

namespace WpfApplication1
{
    public class MainWindowViewModel
    {
        public ReactiveProperty<string> Input { get; private set; }
        public ReactiveProperty<string> Output { get; private set; }
        public ReactiveCommand ResetInputCommand { get; private set; }

        public MainWindowViewModel()
        {
            this.Input = new ReactiveProperty<string>();
            this.Output = this.Input
                .Where(s => !string.IsNullOrWhiteSpace(s))
                .Delay(TimeSpan.FromSeconds(5))
                .Select(s => s.ToUpper())
                .ToReactiveProperty();

            this.ResetInputCommand = new ReactiveCommand();
            this.ResetInputCommand.Subscribe(_ => this.Input.Value = "Hello world.");
        }
    }
}