【发布时间】:2023-04-04 07:53:02
【问题描述】:
我正在尝试通过调整 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#
【问题讨论】:
-
您还应该在此处包含
InvalidCastException,因为它可能会阐明问题。 -
我添加了我收到的消息的屏幕截图。恐怕除了“指定的演员表无效”之外,它并没有给我太多信息。 UpdateSourceTrigger 返回一个 Binding
对象。 -
我应该补充一点,TextView 的绑定有效。 '_bindings.Add(this.SetBinding( () => Vm.TapSelectionText, () => TapSelectionLabel.Text));'
标签: c# android mvvm xamarin.android mvvm-light