Binding a boolean (HasLiked) to Pressed from viewmodel results in error:
Failed to create target binding for from HasLiked to Pressed
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?
EDIT- binding:
<RelativeLayout
style="@style/ButtonExtraSocial"
android:background="@drawable/SelectorButtonExtra"
app:MvxBind="Click LikeCommand; Pressed HasLiked">
....
</RelativeLayout>
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:
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.
Thank you Stuart and Cheesebaron for your answers but I found out what was the real cause of this problem: packaging settings in Xamarin studio: ´Use shared mono runtime´ has to be enabled (I think by default it is checked, but I had it unchecked).
You can access packaging settings by right click on project (not solution!) > properties > Android Build > Use shared Mono runtime.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.