我似乎无法在这里重现您的问题。我创建了一个非常简单的示例,可以满足您的要求。
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); }
}
}
第一视图.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;
}
}
第一视图.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>
这会产生这两种不同的视觉状态:
如您所见,它以IsPressed
真实开始,当我单击按钮时,它变为虚假。我已经证实它也可以反过来工作。