使用AppCompatEditText进行数据绑定在Release版本中不起作用

时间:2017-04-23 12:24:30

标签: android xamarin mvvmcross

我有以下视图,其中AppCompatEditText的内容应该绑定到ViewModel的“ArticleName”属性。这在调试版本中工作正常,但在版本构建中没有。这种效果的原因是什么?这是一个非常基本的ViewModel对话框。

<RelativeLayout xmlns:local="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:minWidth="25px"
    android:minHeight="25px">
    <TextView
        android:id="@+id/txtHeadline"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_vertical_margin"
        android:textAppearance="@android:style/TextAppearance.DialogWindowTitle"
        android:text="New item" />
    <AppCompatEditText
        android:id="@+id/editArticleName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtHeadline"
        local:MvxBind="Text ArticleName" />
    <Button
        android:id="@+id/btnAdd"
        android:text="Add"
        android:layout_below="@id/editArticleName"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimary"
        local:MvxBind="Click AddItem;Enabled CanAddItem"
        style="?android:attr/borderlessButtonStyle" />

ViewModel:

public class MyViewModel : MvxViewModel
    {
        private string m_ArticleName;

        public string ArticleName
        {
            get
            {
                return m_ArticleName;
            }

            set
            {
                m_ArticleName = value;
                RaiseAllPropertiesChanged();

                // this setter is never called
            }
        }

        public MvxCommand AddItem
        {
            get;
            private set;
        }

        public bool CanAddItem
        {
            get
            {
                return !String.IsNullOrEmpty(m_ArticleName);
            }
        }

        public MyViewModel()
        {
            AddItem = new MvxCommand(doAddItem);
        }

        private void doAddItem()
        {
           // is never called in releases build because CanAddItem is never true
        }
    }

我还在LinkerPleaseInclude.cs中添加了以下语句,但没有效果:

    public void Include(AppCompatEditText text)
    {
        text.TextChanged += (sender, args) => text.Text = "" + text.Text;
        text.Hint = "" + text.Hint;
        text.Text = "Test";
    } 

编辑:

如果我将AppCompatEditText与常规EditText交换,会出现同样的效果。

1 个答案:

答案 0 :(得分:1)

在MvvmCross中,文本绑定的事件已更改为使用AfterTextChanged

因此,您可以在LinkerPleaseInclude.cs

中进行以下更改

text.AfterTextChanged += (sender, args) => text.Text = "" + text.Text;