MvxCommand绑定改变布局屏幕

本文关键字:屏幕 布局 改变 绑定 MvxCommand | 更新日期: 2023-09-27 18:14:15

所以在我的项目中,我使用MvvXCross和PCL为我的Xamarin。机器人项目。

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

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

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

我如何能够创建一个命令,将首先调用一个方法和评估它,并根据评估将发送用户到另一个布局或使用户停留在相同的布局与错误信息?

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

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> ());
        }
    }
}

My Login Layout

<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>

MvxCommand绑定改变布局屏幕

你应该调用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>