Skip to content Skip to sidebar Skip to footer

Android View States: Pressed, Activated, Selected Etc. Bindings. Do I Have To Write Custom Bindings?

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 o

Solution 1:

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

FirstViewModel.cs

publicclassFirstViewModel 
    : MvxViewModel
{
    publicFirstViewModel()
    {
        IsPressed = true;
    }

    privatestring _hello = "Hello MvvmCross";
    publicstring Hello
    { 
        get { return _hello; }
        set { _hello = value; RaisePropertyChanged(() => Hello); }
    }

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

FirstView.cs

[Activity(Label = "View for FirstViewModel")]
publicclassFirstView : MvxActivity
{
    publicnew FirstViewModel ViewModel
    {
        get { return (FirstViewModel)base.ViewModel; }
        set { base.ViewModel = value; }
    }

    protectedoverridevoidOnCreate(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"?><LinearLayoutxmlns: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"><EditTextandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:textSize="40dp"local:MvxBind="Text Hello"
    /><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:textSize="40dp"local:MvxBind="Text Hello"
    /><Buttonandroid:id="@+id/button"android:layout_width="fill_parent"android:layout_height="wrap_content"local:MvxBind="Text Hello; Pressed IsPressed"/><RelativeLayoutandroid: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"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@drawable/bg_pressed"android:state_pressed="true"/><itemandroid:drawable="@drawable/bg_normal"/></selector>

bg_normal.xml

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

bg_pressed.xml

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

This produces these two different visual states:

First StateSecond State

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.

Solution 2:

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.

Post a Comment for "Android View States: Pressed, Activated, Selected Etc. Bindings. Do I Have To Write Custom Bindings?"