【问题标题】:MvvmCross Android binding EditText in release mode发布模式下的 MvvmCross Android 绑定 EditText
【发布时间】:2019-12-04 20:49:13
【问题描述】:

我在 Android 平台上绑定 EditText 时遇到问题。 今天我将我的项目 MvvmCross 框架从 6.2.X 更新到 6.3.1(+ 更新其他 NuGet),并将 TargetSdk 和 CompileSdk 从 Android 8.1 更改为 9.0,现在当我将发布模式和链接设置为“Sdk 程序集”时,只有我的应用程序在我绑定 EditText 的视图上崩溃。在我检查了“使用共享运行时”并将链接设置为“无”的调试中,它没有问题。

我在 LinkerPleaseInclude 中包含 TextView:

public void Include(TextView text)
{
    text.AfterTextChanged += (sender, args) => text.Text = $"{text.Text}";
    text.Hint = $"{text.Hint}";
}

它抛出这个异常:https://pastebin.com/EmkuL7hM

布局:

   <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:fillViewport="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:paddingTop="50dp"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/backgroundColor">
       <LinearLayout
            android:layout_margin="10dp"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
            <ImageView
                android:id="@+id/logo"
                android:layout_width="200dp"
                android:layout_height="100dp"
                android:src="@drawable/ic_logo_red"
                android:scaleType="fitCenter" />
        </LinearLayout>
       <LinearLayout
            android:layout_marginTop="10dp"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
            <TextView
                android:text="Email"
                local:MvxLang="Text Email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/primaryColor"
                android:textStyle="bold"
                android:textSize="@dimen/text_medium" />
            <EditText
                android:layout_marginTop="5dp"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                local:MvxBind="Text LoginName"
                android:singleLine="true"
                android:inputType="textEmailAddress"
                android:background="@color/white"
                android:cursorVisible="true"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColorHint="@color/primaryTextColor"
                android:textColor="@color/primaryTextColor" /> 

            </LinearLayout>
            <LinearLayout
            android:layout_marginTop="10dp"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
            <TextView
                android:text="Password"
                local:MvxLang="Text Password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/primaryColor"
                android:textStyle="bold"
                android:textSize="@dimen/text_medium" />
            <EditText
                android:layout_marginTop="5dp"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                local:MvxBind="Text Password"
                android:singleLine="true"
                android:inputType="textPassword"
                android:background="@color/white"
                android:cursorVisible="true"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColorHint="@color/primaryTextColor"
                android:textColor="@color/primaryTextColor" />
        </LinearLayout>
        <LinearLayout
            android:layout_marginTop="20dp"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
            <Button
                android:gravity="center"
                android:id="@+id/loginButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="@dimen/text_large"
                local:MvxLang="Text Login"
                local:MvxBind="Click LoginCommand"
                android:padding="10dp"
                android:background="@drawable/button_round_primary"
                android:textColor="@color/white"
                android:text="Login"
                android:textStyle="bold" />
        </LinearLayout> 
    </LinearLayout>
</ScrollView>

视图模型:

  public class LoginViewModel : MvxViewModel
    {
        private readonly IMvxNavigationService _navigationService;
        private readonly IInfoMessageReporter _infoMessageReporter;
        private readonly ISessionInfo _session;
        private readonly ILoginService _loginService;
        private readonly IDataService _dataService;

        public LoginViewModel()
        {
            _navigationService = Mvx.IoCProvider.Resolve<IMvxNavigationService>();
            _infoMessageReporter = Mvx.IoCProvider.Resolve<IInfoMessageReporter>();
            _session = Mvx.IoCProvider.Resolve<ISessionInfo>();
            _loginService = Mvx.IoCProvider.Resolve<ILoginService>();
            _dataService = Mvx.IoCProvider.Resolve<IDataService>();

            RememberLogin = true;
        }

        public IMvxLanguageBinder TextSource => new MvxLanguageBinder(Constants.LocalizationNamespace, GetType().Name);

        public override async Task Initialize()
        {
            await InitializePermissionsAsync();
        }

        private async Task InitializePermissionsAsync()
        {
            // some stuff...
        }

        private string _password;
        public string Password
        {
            get => _password;
            set
            {
                _password = value;
                RaisePropertyChanged(() => Password);
            }
        }

        private string _loginName;
        public string LoginName
        {
            get => _loginName;
            set
            {
                _loginName = value;
                RaisePropertyChanged(() => LoginName);
            }
        }

        private MvxAsyncCommand _loginCommand;
        public IMvxAsyncCommand LoginCommand
        {
            get
            {
                _loginCommand = _loginCommand ?? new MvxAsyncCommand(async () => await ExecuteLoginAsync());
                return _loginCommand;
            }
        }

        private async Task ExecuteLoginAsync()
        {
            // some stuff....
        }
    }

【问题讨论】:

  • 您在问题中提到了编辑文本,但是,您显示的代码是针对 TextView 的。您在 LinkerPleaseInclude 中是否有类似的方法用于 EditText?
  • 我尝试为 EditText 创建 Include 但我读到它不需要,因为 EditText 继承自 TextView。使用 Include for EditText 也有同样的问题。
  • 你能贴出你使用绑定和 ViewModel 的代码吗?
  • 我编辑并添加了 ViewModel 和 Layout。

标签: xamarin binding xamarin.android linker mvvmcross


【解决方案1】:

我相信您遇到的问题是 Xamarin 最新版本(围绕 Xamarin Android 9.4)中的当前错误。可以在 GitHub 上跟踪此问题 here


建议的解决方法

万一其他用户在使用时遇到此问题 Xamarin.Android 9.4,一种可能的解决方法是使用自定义链接器 配置以保留缺少的类型。为此,添加一个新的 linker.xml 文件到项目中,将 Build Action 设置为 LinkDescription,并添加 XML 行以保留缺少的类型。 例如,对于 ITextWatcherInvoker 错误,添加以下内容 文件的行:

<linker>
 <assembly fullname="Mono.Android">
   <type fullname="Android.Text.ITextWatcherInvoker" preserve="all" />
 </assembly>
</linker>

【讨论】:

    猜你喜欢
    • 2017-04-13
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多