protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); _viewModel = new LapViewModel(this.Application as IModelPool); // Set our view from the "main" layout resource SetContentView(Resource.Layout.activity_lap); // ListView(listLaps, ArrayAdapter) のバインド // 番号付きのリストに変換 var numberedLaps = _viewModel.FormattedLaps.ToNumberedLaps(); var listLaps = FindViewById <ListView>(Resource.Id.listLaps); var listAdapter = new ArrayAdapter(this, global::Android.Resource.Layout.SimpleListItem1); listLaps.Adapter = listAdapter; listAdapter .SetBinding(numberedLaps) .AddTo(_subscriptionOnCreate); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); _viewModel = new LapViewModel(this.Application as IModelPool); // Set our view from the "main" layout resource SetContentView(Resource.Layout.activity_lap); // ListView(listLaps, ArrayAdapter) のバインド // フォーマットされた経過時間群を表す Observable(time と timeFormat のどちらかが変更されたら更新) var formattedLaps = _viewModel.Laps.CombineLatest( _viewModel.TimeFormat, (laps, f) => laps.Select((x, i) => $"{i+1}. {TimeSpan.FromMilliseconds(x).ToString(f)}")) .ToReactiveProperty(); var listLaps = FindViewById <ListView>(Resource.Id.listLaps); var listAdapter = new ArrayAdapter(this, global::Android.Resource.Layout.SimpleListItem1); listLaps.Adapter = listAdapter; listAdapter .SetBinding(formattedLaps) .AddTo(_subscriptionOnCreate); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); _viewModel = new MainViewModel(this.Application as IModelPool); // Set our view from the "main" layout resource SetContentView(Resource.Layout.activity_main); // TextView(textTime) のバインド FindViewById <TextView>(Resource.Id.textTime) .SetBinding(v => v.Text, _viewModel.FormattedTime.ObserveOnUIDispatcher().ToReactiveProperty()) .AddTo(_subscriptionOnCreate); // Button(buttonStartStop) のバインド var buttonStartStop = FindViewById <Button>(Resource.Id.buttonStartStop); buttonStartStop.ClickAsObservable() .SetCommand(_viewModel.CommandStartOrStop) .AddTo(_subscriptionOnCreate); buttonStartStop .SetBinding(v => v.Text, _viewModel.IsRunning.Select(x => x ? "STOP" : "START") .ObserveOnUIDispatcher() .ToReactiveProperty()) .AddTo(_subscriptionOnCreate); // Button(buttonLap) のバインド var buttonLap = FindViewById <Button> (Resource.Id.buttonLap); buttonLap .ClickAsObservable().SetCommand(_viewModel.CommandLap) .AddTo(_subscriptionOnCreate); buttonLap.SetBinding( v => v.Enabled, _viewModel.IsRunning) .AddTo(_subscriptionOnCreate); // Switch(switchVisibleMillis) のバインド var switchVisibleMillis = FindViewById <Switch>(Resource.Id.switchVisibleMillis); switchVisibleMillis .SetBinding(v => v.Checked, _viewModel.IsVisibleMillis .ObserveOnUIDispatcher() .ToReactiveProperty()) .AddTo(_subscriptionOnCreate); switchVisibleMillis.ClickAsObservable() .SetCommand(_viewModel.CommandToggleVisibleMillis) .AddTo(_subscriptionOnCreate); // ListView(listLaps, ArrayAdapter) のバインド // 番号付きのリストに変換 var numberedLaps = _viewModel.FormattedLaps.ToNumberedLaps(); var listLaps = FindViewById <ListView>(Resource.Id.listLaps); var listAdapter = new ArrayAdapter(this, global::Android.Resource.Layout.SimpleListItem1); listLaps.Adapter = listAdapter; listAdapter .SetBinding(numberedLaps) .AddTo(_subscriptionOnCreate); // ■ViewModel からの Message の受信 // 画面遷移のメッセージ受信 _viewModel.Messenger.Register(typeof(StartViewMessage).Name, message => RunOnUiThread(() => { var app = this.ApplicationContext as App; var m = message as StartViewMessage; if (app.VmToActivityTypes.ContainsKey(m.ViewModelType)) { var intent = new Intent(this, app.VmToActivityTypes[m.ViewModelType]); this.StartActivity(intent); } })); // トースト表示のメッセージ受信 _viewModel.Messenger.Register(typeof(ShowToastMessage).Name, message => RunOnUiThread(() => { var m = message as ShowToastMessage; Toast.MakeText(this, m.Text, ToastLength.Long).Show(); })); }