xamarin - Release模式下的 mvvmcross 绑定(bind)

标签 xamarin xamarin.android mvvmcross

我已经使用 MvvMCross 一段时间了,这段代码一直在工作。我确实安装了最新的更新,但我不能发誓这是问题开始的时候。发生的事情是我有一个登录屏幕,其中包含与用户名和密码的简单文本绑定(bind)。当我使用使用共享运行时进行编译时,它工作正常。如果未选中此项,则不会将文本绑定(bind)到文本字段。我的布局如下:

<?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">
<LinearLayout
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="50dp"
    android:background="@drawable/borderdoublewidth">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            style="@style/InputTextView"
            android:text="User Name" />
        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/txtUserName"
            style="@style/InputEditText"
            local:MvxBind="Text UserName" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            style="@style/InputTextView"
            android:text="Password" />
        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/txtPassword"
            android:inputType="textPassword"
            android:password="true"
            style="@style/InputEditText"
            local:MvxBind="Text Password" />
    </LinearLayout>
    <Button
        android:text="Login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        local:MvxBind="Click CheckLogin" />
</LinearLayout>
</LinearLayout>

我的 View 模型如下:
class LoginViewModel : MvxViewModel
{

    public void Init()
    {
    }

    public override void Start()
    {
        base.Start();

        Task.Run(async () =>
        {
            var l = await ListDataSource.GetLocations();
            Locations = l.Location.ToArray<string>();
        });
    }

    public string Location
    {
        get
        {
            return Settings.Location;
        }
        set
        {
            Settings.Location = value;
            RaisePropertyChanged(() => Location);
        }
    }

    private string[] _Locations;
    public string[] Locations
    {
        get
        {
            return _Locations;
        }
        set
        {
            _Locations = value;
            RaisePropertyChanged(() => Locations);
        }
    }

    private string _UserName;
    public string UserName
    {
        get
        {
            return _UserName;
        }
        set
        {
            _UserName = value;
            RaisePropertyChanged(() => UserName);
            EventLog.Debug("UserName Changed <" + _UserName + ".");
        }
    }

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

    public IMvxCommand CheckLogin
    {
        get
        {
            return new MvxCommand(() =>
                ValidateDriver()
                );
        }
    }

    private bool LoggingIn = false;
    private async void ValidateDriver()
    {
        if (Settings.Location == null)
        {
            MiscFunctions.messageBox("Please set the Location!");
            return;
        }
        if (LoggingIn)
            return;

        LoggingIn = true;

        //btnLogin.Focus(Windows.UI.Xaml.FocusState.Programmatic);
        string VersionName = "";
        MPS_Mobile_Warehouse.Droid.Views.LoginView act = (Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity as MPS_Mobile_Warehouse.Droid.Views.LoginView) ?? null;
        if (act != null)
        {
            VersionName = act.GetAppVersionName();
        }

        //DriverName gets set in this call.
        if (await ShipmentDataSource.CheckTabletUser(UserName, Password, VersionName))
        {
            ShowViewModel<MainViewModel>();
        }
        LoggingIn = false;
    }


}

与 CheckLogin Button 绑定(bind)的绑定(bind)似乎工作正常。当它调用 CheckTabletUser 时,UserName 和 Password 属性为空。

我正在编辑它,以便人们可以看到我的 LinkerPleaseInclude.cs 文件。它是 MvvmCross Starter Pack 附带的原版:
public class LinkerPleaseInclude
{
    public void Include(Button button)
    {
        button.Click += (s, e) => button.Text = button.Text + "";
    }

    public void Include(CheckBox checkBox)
    {
        checkBox.CheckedChange += (sender, args) => checkBox.Checked = !checkBox.Checked;
    }

    public void Include(Switch @switch)
    {
        @switch.CheckedChange += (sender, args) => @switch.Checked = !@switch.Checked;
    }

    public void Include(View view)
    {
        view.Click += (s, e) => view.ContentDescription = view.ContentDescription + "";
    }

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

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

    public void Include(CompoundButton cb)
    {
        cb.CheckedChange += (sender, args) => cb.Checked = !cb.Checked;
    }

    public void Include(SeekBar sb)
    {
        sb.ProgressChanged += (sender, args) => sb.Progress = sb.Progress + 1;
    }

    public void Include(Activity act)
    {
        act.Title = act.Title + "";
    }

    public void Include(INotifyCollectionChanged changed)
    {
        changed.CollectionChanged += (s, e) => { var test = $"{e.Action}{e.NewItems}{e.NewStartingIndex}{e.OldItems}{e.OldStartingIndex}"; };
    }

    public void Include(ICommand command)
    {
        command.CanExecuteChanged += (s, e) => { if (command.CanExecute(null)) command.Execute(null); };
    }

    public void Include(MvvmCross.Platform.IoC.MvxPropertyInjector injector)
    {
        injector = new MvvmCross.Platform.IoC.MvxPropertyInjector();
    }

    public void Include(System.ComponentModel.INotifyPropertyChanged changed)
    {
        changed.PropertyChanged += (sender, e) => {
            var test = e.PropertyName;
        };
    }

    public void Include(MvxTaskBasedBindingContext context)
    {
        context.Dispose();
        var context2 = new MvxTaskBasedBindingContext();
        context2.Dispose();
    }
}

我通过恢复到 MvvmCross 版本 4.3.0 解决了我的问题。如果开发人员需要知道最新版本中发生了什么,我将保持打开状态。

最佳答案

MvvmCross 使用 AfterTextChanged事件而不是 TextChanged监听 EditText/TextView 的变化。在您的 LinkerPleaseInclude.cs您只需要更改为使用 AfterTextChanged事件。那么您应该对 v4.4 没有任何问题。

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

关于xamarin - Release模式下的 mvvmcross 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40874131/

相关文章:

android - 如何实现Expandablelistview子自定义布局?

android - MvvmCross Android - ViewModel 从未被垃圾收集

c# - 在 Xamarin PCL 中获取线程信息

android - 什么是 com.android.externalstorage?

c# - Monodroid EditText 输入类型编号检测输入按键

mvvmcross - MvxSimpleIoCContainer 和多个具体实现

c# - 为什么在使用 Microsoft.Bcl 时不能在我的 Windows Phone 7.1 MvvmCross 项目中使用 await 关键字 - 不能等待 'System.Threading.Tasks.Task?

ios - 在 Mac 上的 Xamarin Studio 中加载 iOS 解决方案时出现问题

android - BottomSheet - 不正确的设计行为

ios - iOS 9.0-9.2 中 iOS 授权文件中的长域列表