hirakun-jpの日記

日記を書きます

MVVM試した2

この記事をほぼ写経しました。計算ができた!

WPF/C#/MVVM/Prismで、簡単なWPFアプリケーションを作ります [C#][WPF] - nprogram’s blog

ViewModelを残しとく。

using Prism.Commands;
using Prism.Mvvm;

namespace MrNprogramExample
{
    class ViewModel: BindableBase
    {
        private Model _model;
        public ViewModel()
        {
            _model = new Model();
        }

        private string _leftValue;
        public string LeftValue
        {
            get { return _leftValue; }
            set { this.SetProperty(ref _leftValue, value); }
        }

        private string _rightValue;
        public string RightValue
        {
            get { return _rightValue; }
            set { this.SetProperty(ref _rightValue, value); }
        }

        private string _answerValue;
        public string AnswerValue
        {
            get { return _answerValue; }
            set { this.SetProperty(ref _answerValue, value); }
        }

        #region コマンド
        private DelegateCommand _concatCommand;
        public DelegateCommand ConcatCommand
        {
            get { return _concatCommand = _concatCommand ?? new DelegateCommand(ConcatExecute); }
        }

        private void ConcatExecute()
        {
            AnswerValue = _model.Concat(LeftValue, RightValue);
        }
        #endregion

    }
}

f:id:hirakun-jp:20180606162309p:plain

前回記事MVVM試した - hirakun-jpの日記で参考(ほぼ写経ですすみません)にさせてもらったコードだとコマンドクラスが別にあったけど、これはViewModelの中に入ってるみたい。 いろんな書き方ができるのだなあ。

XAMLの書き方でstar value知らなかったので勉強なった。 このブログhttp://blog.okazuki.jp/entry/20130106/1357483477で勉強したい。

BindingにあるOneWayについて、この記事 今さら入門するMVVMに必要な技術要素(Xamarin.Forms & UWP) - かずきのBlog@hatena

で理解できました。

Bindingには、Modeがあります。Modeには以下のようなものがあります。 OneWay: ソース(C#のオブジェクト)からターゲット(画面のコントロール側)への1方向同期 TwoWay: ソースとターゲットの双方向同期 OneTime: ソースからターゲットへの1度きりの同期 指定しない場合は、デフォルトでOneWayになります。

ということらしい。