本文介绍了Android的视图状态:按下,激活,选择等绑定。我必须编写自定义的绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

绑定一个布尔(HasLiked),以错误的视图模型结果的追问:

Binding a boolean (HasLiked) to Pressed from viewmodel results in error:

Failed to create target binding for from HasLiked to Pressed

这是否意味着Android的View.Pressed(和其他国家)通过MvxBind绑定不支持默认? ?我需要编写自定义绑定

Does it mean that android View.Pressed (and other states) bindings by MvxBind aren't supported by default? Do i need to write custom bindings?

编辑 - 绑定:

<RelativeLayout
        style="@style/ButtonExtraSocial"
        android:background="@drawable/SelectorButtonExtra"
        app:MvxBind="Click LikeCommand; Pressed HasLiked">
....
</RelativeLayout>



HasLiked布尔在视图模型存在(当我把它绑定到TextView的文本,我得到的值)。

HasLiked boolean exist in viewmodel (when I bind it to TextView text, I get the value).

推荐答案

我似乎无法重现在这里你的问题。我创建了一个非常简单的示例,它做你问什么。

I can't seem to reproduce your problem here. I have created a very simple sample, which does what you ask for.

FirstViewModel.cs

public class FirstViewModel
    : MvxViewModel
{
    public FirstViewModel()
    {
        IsPressed = true;
    }

    private string _hello = "Hello MvvmCross";
    public string Hello
    {
        get { return _hello; }
        set { _hello = value; RaisePropertyChanged(() => Hello); }
    }

    public bool _isPressed;
    public bool IsPressed
    {
        get { return _isPressed; }
        set { _isPressed = value; RaisePropertyChanged(() => IsPressed); }
    }
}



FirstView.cs

[Activity(Label = "View for FirstViewModel")]
public class FirstView : MvxActivity
{
    public new FirstViewModel ViewModel
    {
        get { return (FirstViewModel)base.ViewModel; }
        set { base.ViewModel = value; }
    }

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.FirstView);

        var button = FindViewById<Button>(Resource.Id.button);
        button.Click += (sender, args) => ViewModel.IsPressed = !ViewModel.IsPressed;
    }
}



FirstView.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  <EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="40dp"
    local:MvxBind="Text Hello"
    />
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="40dp"
    local:MvxBind="Text Hello"
    />
  <Button
    android:id="@+id/button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    local:MvxBind="Text Hello; Pressed IsPressed"/>
  <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:background="@drawable/bg"
    local:MvxBind="Pressed IsPressed"/>
</LinearLayout>



bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/bg_pressed"
      android:state_pressed="true"/>
  <item android:drawable="@drawable/bg_normal"/>
</selector>



bg_normal.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <solid android:color="#FF0000" />
</shape>



bg_pressed.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <solid android:color="#FF00FF" />
</shape>

这会产生两种不同的视觉状态:

This produces these two different visual states:


正如你看到它与 IsPressed 是真实,当我按一下按钮就变成假的。我已验证它的工作原理周围还有其他方式。

As you see it starts with IsPressed being true and when I click the button it becomes false. I have verified that it works the other way around as well.

这篇关于Android的视图状态:按下,激活,选择等绑定。我必须编写自定义的绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:30