Android视图状态:按下,激活,选择等绑定。我是否必须编写自定义绑定?

时间:2013-07-29 14:42:38

标签: c# android xamarin.android mvvmcross

将布尔值(HasLiked)绑定到从viewmodel按下会导致错误:

Failed to create target binding for from HasLiked to Pressed

这是否意味着默认情况下不支持MvxBind的android View.Pressed(和其他状态)绑定?我需要编写自定义绑定吗?

EDIT-binding:

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

在viewmodel中存在HasLiked布尔值(当我将其绑定到TextView文本时,我得到了值)。

2 个答案:

答案 0 :(得分:4)

我似乎无法在这里重现你的问题。我创建了一个非常简单的示例,它可以满足您的要求。

<强> 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>

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

First State Second State

正如您所看到的那样,IsPressed为真,当我点击按钮时,它会变为假。我已经证实它也是相反的。

答案 1 :(得分:1)

感谢Stuart和Cheesebaron的答案,但我发现了这个问题的真正原因:Xamarin工作室的包装设置:'使用共享单声道运行时'必须启用(我认为默认情况下会检查,但是我没试过它。)

您可以通过右键单击项目(不是解决方案!)来访问打包设置。属性&gt; Android Build&gt;使用共享的Mono运行时。