我试图通过调整SeekBar来更改TextView中的文本。如果我直接设置TapSelectionProgress属性(TapSelectionProgress = 2;),则TextView将更新。但是,绑定(bind)更新永远不会从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#

最佳答案

我能够使它工作,尽管不是我想要的方式。尝试SetCommand时,我抛出了相同的异常,因此我稍微破坏了MVVM体系结构。

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

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

现在移动滑块可更新属性并触发UI更新。

关于c# - MVVM灯光绑定(bind)SeekBar的进度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42985962/

10-12 19:47