我想实现我的xamarin移动项目MVVM模式。使用MVVM模式,而无需使用现有的MVVM页面导航构架
我有以下文件MVVM
- LoginView
- LoginViewModel
- BaseViewModel
以下是我LoginViewModel
public class LoginViewModel : BaseViewModel
{
private bool isLoginIndicator= false;
private string etUserName;
private string etPassword;
public LoginViewModel()
{
OnLogin = new Command(doLogin ,()=>!LoginIndicator);
MessagingCenter.Subscribe<IMessage, EventType>(this, RestApi.UI_EVENT, (sender, eventType) =>
{
LoginIndicator = false;
if (eventType.status)
{
Application.Current.MainPage.DisplayAlert(AppResources.success, "Login done", "Ok");
}
else
{
Application.Current.MainPage.DisplayAlert(AppResources.failed, eventType.errorMessage, "Ok");
}
});
}
public bool LoginIndicator
{
get { return isLoginIndicator; }
set
{
isLoginIndicator = value;
OnPropertyChanged("LoginIndicator");
OnLogin.ChangeCanExecute();
}
}
public string UserName
{
get { return etUserName; }
set
{
etUserName = value;
OnPropertyChanged("UserName");
}
}
public string Password
{
get { return etPassword; }
set
{
etPassword = value;
OnPropertyChanged("Password");
}
}
public Command OnLogin { get; }
void doLogin()
{
LoginIndicator = true;
UserRequest user = new UserRequest();
user.userName = etUserName;
user.password = etPassword;
user.companyId = "CEE";
user.appVersion = Constants.getAppVersion();
user.osVersion = Constants.getOSVersion();
user.deviceId = Constants.getDeviceModel() + " " + Constants.getDevicePlatform();
new RestApi().userLogin(JsonConvert.SerializeObject(user));
}
}
该类通常使得Web服务调用时OnLogin
命令从按钮激发和Message
使用MessageCenter
现在我想转到我的MainPage
这是母版页一旦用户成功,因此我需要导航到母版页时eventType.status
是真实的记录播出内Message Subscriber
,但我不知道我怎样才能正确地根据MVVM模式切换不同页面。
我试图在网上搜索,我发现有可用像MVVMCross
和MVVMLight
等现成的框架,但我不希望使用那些依赖条件,并愿意执行导航一些其他的方式,如果任何人都可以提出
Application.Current.MainPage =新MyFirstPageAfterLogin();它不正确? –
它会是一个适当的方式来做到这一点在MVVM模式?不知道虽然:) – Hunt
我不知道...我认为你应该从ViewModel发送一个消息中心视图和视图使用Application.Current.MainPage =新...但我不知道。我不知道MVVM非常好... –