我尝试通过调整SeekBar来更改TextView中的文本。如果我直接设置TapSelectionProgress属性(TapSelectionProgress = 2;
),TextView会更新。但是,绑定更新永远不会从UI触发。
在Main.axml中:
<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" />
在MainViewModel.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);
}
}
在MainActivity.cs中:
_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 :(得分: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更新。
答案 1 :(得分:0)
对你来说可能为时已晚,但我刚才有同样的问题 - 挖掘MVVM光源给了我真实解决方案的暗示。您需要为ObserveSourceEvent设置EventArgs的类型(UpdateSourceTrigger的新名称),如下所示:
.ObserveSourceEvent<ProgressChangedEventArgs>(nameof(SeekBar.ProgressChanged)));