2017-03-23 4 views
0

SeekBarを調整してTextViewのテキストを変更しようとしています。 TapSelectionProgressプロパティを直接設定すると(TapSelectionProgress = 2;)、TextViewが更新されます。ただし、バインディングの更新はUIからトリガーされることはありません。 Main.axmlでMVVMライトバインディングSeekBarの進捗

:MainViewModel.csで

<SeekBar 
    android:id="@+id/tapSeekBar" 
    android:max="5" 
    android:layout_width="300dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:rotation="270" 
    android:padding="50dp" /> 

:MainActivity.csで

public int TapSelectionProgress 
{ 
    get 
    { 
     return _tapSelectionProgress; 
    } 
    set 
    { 
     Set(ref _tapSelectionProgress, value); 
     if (value == 0) 
     { 
      TapSelectionText = "Tap Selection: Off"; 
     } 
     else 
     { 
      TapSelectionText = String.Format("Tap Selection: {0}", value); 
     }     
    } 
} 

public string TapSelectionText 
{ 
    get 
    { 
     return _tapSelectionText; 
    } 
    set 
    { 
     Set(ref _tapSelectionText, value); 
    } 
} 

_bindings.Add(this.SetBinding(
      () => TapSeekBar.Progress, 
      () => Vm.TapSelectionProgress)); 

私はソース・トリガーを更新しようとしました:

_bindings.Add(this.SetBinding(
      () => TapSeekBar.Progress, 
      () => Vm.TapSelectionProgress).UpdateSourceTrigger("ProgressChanged")); 

But this gives me an InvalidCastException

BindingModeをTwoWayに設定してみました。私はSeekBarでMVVM Lightのサンプルを見つけることができませんでした。どんな助けもありがとうございます。

私はそれを動作させることができました
0xFFFFFFFFFFFFFFFF in System.Diagnostics.Debugger.Mono_UnhandledException_internal C# 
0x1 in System.Diagnostics.Debugger.Mono_UnhandledException at /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/corlib/System.Diagnostics/Debugger.cs:122,-1 C# 
0x26 in object.1f29ec3d-529a-4f23-9cce-3fd0f5ac1e00 C# 
0x2F in System.Reflection.EventInfo.AddEventFrame<Android.Widget.SeekBar, System.EventHandler<Android.Widget.SeekBar.ProgressChangedEventArgs>> at /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/corlib/System.Reflection/EventInfo.cs:222,-1 C# 
0x7C in System.Reflection.EventInfo.AddEventHandler at /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/corlib/System.Reflection/EventInfo.cs:110,-1 C# 
0xC5 in GalaSoft.MvvmLight.Helpers.Binding<int,int>.UpdateSourceTrigger at c:\MvvmLight\Source\GalaSoft.MvvmLight\GalaSoft.MvvmLight.Platform (Android)\Helpers\BindingGeneric.cs:342,13 C# 
0x172 in MDP_Demo.MainActivity.OnCreate at c:\hgroot\Mobile\MDP Demo\MDP Demo\MainActivity.cs:51,13 C# 
0x13 in Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ at /Users/builder/data/lanes/3511/501e63ce/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.App.Activity.cs:2102,4 C# 
0x17 in object.1f29ec3d-529a-4f23-9cce-3fd0f5ac1e00 C# 
+0

ます。また、ここでは 'InvalidCastException'を含める必要があります:あなたはこのように、ObserveSourceEvent(UpdateSourceTriggerの新しい名前)のためにEventArgsのタイプを設定する必要があります。 – Xiaoy312

+0

受け取ったメッセージのスクリーンショットを追加しました。私は "指定されたキャストは有効ではありません"以外の多くの情報を私に与えてくれるのではないかと心配しています。 UpdateSourceTriggerはBinding オブジェクトを返します。 –

+0

私はTextViewのバインディングが動作することを追加する必要があります。 '_bindings.Add(this.SetBinding( ()=> Vm.TapSelectionText、 ()=> TapSelectionLabel.Text));' –

答えて

2

、私が好きだろうではない道かかわら:

ここでは例外中のコールスタックです。私はSetCommandを試みたときに同じ例外がスローされたので、MVVMアーキテクチャを少し壊しました。今スライダーを動かす

TapSeekBar.ProgressChanged += new EventHandler<SeekBar.ProgressChangedEventArgs>(TapSeekBar_ProgressChanged); 

...

private void TapSeekBar_ProgressChanged(object sender, SeekBar.ProgressChangedEventArgs e) 
{ 
    Vm.TapSelectionProgress = e.Progress; 
} 

は、プロパティを更新し、UIの更新をトリガします。

+0

私は以下の適切な解決策を追加しました – Ryan

0

おそらくあなたには遅すぎますが、私はまもなく同じ問題を抱えました.MVVM光源を掘り下げて実際の解決策のヒントを得ました。それが問題にいくつかの光を当てることとして

  .ObserveSourceEvent<ProgressChangedEventArgs>(nameof(SeekBar.ProgressChanged)));