繁体   English   中英

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

[英]Android view states: pressed, activated, selected etc. bindings. Do I have to write custom bindings?

从视图模型将布尔值(HasLiked)绑定到Pressed会导致错误:

Failed to create target binding for from HasLiked to Pressed

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

编辑绑定:

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

在视图模型中存在HasLiked布尔值(当我将其绑定到TextView文本时,会得到该值)。

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

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>

这将产生以下两种不同的视觉状态:

第一状态第二状态

如您所见,它从IsPressed为true开始,当我单击按钮时,它变为false。 我已经证实它也可以以其他方式工作。

谢谢Stuart和Cheesebaron的回答,但我发现了造成此问题的真正原因:Xamarin studio中的打包设置:必须启用“使用共享单声道运行时”(我认为默认情况下已选中它,但是我已经做到了未选中)。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM