【问题标题】:MvxCommand binding changing layout screensMvxCommand 绑定更改布局屏幕
【发布时间】:2014-09-14 12:02:16
【问题描述】:

所以在我的项目中,我将 MvvXCross 和 PCL 用于我的 Xamarin.Droid 项目。

所以我有一个需要调用 PCL 的登录屏幕,PCL 将调用一个服务来查看用户是否基于 bool 值进行身份验证。

我已将我的布局按钮连接到我的 LoginViewModel,并且我还添加了一个 MvxCommand 以将用户发送到另一个页面。

问题在于,当登录布局被加载时,ICommand 被加载,所以我只能对那里的用户进行身份验证。

我如何能够创建一个命令,该命令将首先调用一个方法并对其进行评估,并根据评估将用户发送到另一个布局或让用户停留在同一布局并显示错误消息?

我已经提供了一些用于登录的视图模型和布局代码。

登录视图模型

public class LoginViewModel
    : MvxViewModel
{
    private readonly ILoginService _loginService;
    public LoginViewModel(ILoginService loginService)
    {
        _loginService = loginService;
    }

    public LoginViewModel()
    {
    }

    //User's username
    private string _username;
    public string Username
    {
        get { return _username; }
        set { _username = value; RaisePropertyChanged(() => Username); }
    }

    //User's password
    private string _password;
    public string Password
    {
        get { return _password; }
        set { _password = value; RaisePropertyChanged(() => Password); }
    }

    public string AuthenticateUser()
    {

        return null;
    }

    public ICommand LoginCommand
    {
        get 
        { 
            return new MvxCommand (() => ShowViewModel<HomeViewModel> ());
        }
    }
}

我的登录布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    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"
    tools:context=".MainActivity"
    android:background="#000000">
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true">
        <ImageView
            android:src="@drawable/synchramed_trans_300"
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="0.0dp"
            android:layout_marginRight="0.0dp" />
    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_alignParentBottom="true">
        <EditText
            android:id="@+id/etUserName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/edittext_top_bg"
            android:padding="10dp"
            android:hint="Email"
            android:textColorHint="#ADA6A6"
            style="@style/DefaultTextBox"
            android:drawableLeft="@drawable/email"
            android:inputType="textEmailAddress" />
        <EditText
            android:id="@+id/etPass"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/edittext_bottom_bg"
            android:layout_marginTop="-2dp"
            android:padding="10dp"
            android:hint="Password"
            android:textColorHint="#ADA6A6"
            android:password="true"
            style="@style/DefaultTextBox"
            android:drawableLeft="@drawable/password"
            android:inputType="textPassword" />
        <LinearLayout
            android:orientation="horizontal"
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout1">
            <CheckBox
                android:text="Remember Me?"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/checkBox1" />
        </LinearLayout>
        <Button
            android:id="@+id/btnSingIn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_margin="4dp"
            android:text="Sign In"
            style="@style/DefaultButtonText"
            android:background="@drawable/button_default_bg"
            local:MvxBind="Click LoginCommand" />
    </LinearLayout>
</RelativeLayout>

【问题讨论】:

    标签: c# xamarin xamarin.android


    【解决方案1】:

    您应该在 LoginCommand 中调用 LoginService.Authenticate() 方法。如果身份验证成功,则可以导航到下一个屏幕,否则会显示错误。

    这是我目前的做法。

    private MvxCommand _signinCommand;
    public ICommand SigninCommand
    {
        get
        {
            _signinCommand = _signinCommand ?? new MvxCommand(DoSignin);
            return _signinCommand;
        }
    }
    
    
    private async void DoSignin()
    {
        try
        {
            if (!Validate())
            {
                return;
            }
    
            IsBusy = true;
            var success = await SigninService.SigninAsync(Email, Password);
    
            if (success)
            {
                Result = "";
                ShowViewModel<HomeViewModel>();
                Close();
                return;
            }
    
            Result = "Invalid email/password. Please try again.";
        }
        catch (Exception ex)
        {
            Result = "Error occured during sign in.";
            Mvx.Error(ex.ToString());
        }
        finally
        {
            IsBusy = false;
        }
    }
    

    编辑:添加布局

    <?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"
        android:padding="16dp">
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="120dp"
            android:src="@drawable/mainlogo" />
        <EditText
            android:minHeight="40dp"
            android:layout_margin="4dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:hint="Email"
            local:MvxBind="Text Email; Error Errors['Email']"
            android:id="@+id/EmailEditText" />
        <EditText
            android:minHeight="40dp"
            android:layout_margin="4dp"
            android:inputType="textPassword"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="Password"
            local:MvxBind="Text Password; Error Errors['Password']"
            android:id="@+id/PasswordEditText" />
        <TextView
            android:minHeight="40dp"
            android:layout_margin="4dp"
            android:text="Result"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/ResultTextView"
            local:MvxBind="Text Result; Visibility HasResult,Converter=Visibility"
            android:textColor="#f00" />
        <Button
            android:height="48dp"
            android:text="Sign in"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            local:MvxBind="Click SigninCommand;Enabled IsBusy,Converter=Inverted"
            android:id="@+id/SigninButton" />
    </LinearLayout>
    

    【讨论】:

    • 我想问 - Close 方法接受 ViewModel 的参数,应该有什么 viewmodel。其次,“结果”如何发送回要显示的布局?
    • Result 只是一个绑定到 TextView 的属性(参见布局)。至于 Close(),那是一种自定义的扩展方法。你应该只使用 Close(this)。
    • 谢谢,这就是我要找的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 2020-03-17
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多