MvxCommand绑定更改布局屏幕

时间:2014-07-23 10:36:45

标签: c# xamarin xamarin.android

所以在我的项目中,我使用MvvXCross和PCL作为我的Xamarin.Droid项目。

所以我有一个需要调用PCL的登录界面,PCL会调用一个服务来查看用户是否根据bool值进行了身份验证。

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

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

我如何能够创建一个首先调用方法并对其进行评估的命令,并根据评估结果将用户发送到另一个布局或使用户保持相同的布局并显示错误消息?

我提供了一些用于登录的viewmodel和layout的代码。

LoginViewModel

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>

1 个答案:

答案 0 :(得分: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>