【问题标题】:MVVM Light Binding SeekBar's ProgressMVVM Light Binding SeekBar 的进展
【发布时间】: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


【解决方案1】:

我能够让它工作,虽然不是我想要的方式。当我尝试 SetCommand 时抛出了同样的异常,所以我稍微破坏了 MVVM 架构。

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

...

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

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

【讨论】:

  • 我在下面添加了正确的解决方案
【解决方案2】:

对您来说可能为时已晚,但我刚才遇到了同样的问题 - 挖掘 MVVM 光源给了我真正解决方案的提示。您需要为 ObserveSourceEvent 设置 EventArgs 的类型(UpdateSourceTrigger 的新名称),如下所示:

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

【讨论】:

    猜你喜欢
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    相关资源
    最近更新 更多