【发布时间】: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