[英]how to close login page and redirect to another page using mvvm light and Modern UI and dependency injection
我嘗試使用mvvm light在wpf中創建登錄表單,如果登錄成功,將重定向到另一個頁面並關閉登錄頁面。
我成功登錄,但是嘗試重定向時仍然無法正常工作。
這是我的代碼
我的觀點
<Button Command="{Binding Authorize}" Width="100" Height="30" Margin="-110,20,0,0" Content="Login" /> /*this to login*/
我的ViewModel
private void AuthorizeUser()
{
User usr = new User();
usr.LoginName = _userName;
usr.LoginPassword = _password;
User user = _accountService.AuthenticationUser(usr, 1);
if (user != null)
{
var app = new MainWindow();
var context = new MainViewModel();
app.DataContext = context;
app.Show();
var test = new RelayCommand<object>((o) => ((Window)o).Close(), (o) => true);
test.RaiseCanExecuteChanged();
}
}
我想重定向到MainWindow,有人可以幫助我嗎?
使用mvvm light上的messenger lib解決此問題。
1)。 成功登錄后,將消息發送到當前視圖2)。 檢查消息3)。 關閉視圖
這個例子
private void AuthorizeUser()
{
User usr = new User();
usr.LoginName = _userName;
usr.LoginPassword = _password;
User user = _accountService.AuthenticationUser(usr, 1);
if (user != null)
{
var app = new MainWindow();
var context = new MainViewModel();
app.DataContext = context;
app.Show();
Messenger.Default.Send(new NotificationMessage("LoginSuccess"));
}
}
鑒於
public LoginUser()
{
InitializeComponent();
Messenger.Default.Register<NotificationMessage>(this, NotificationMessageReceived);
}
private void NotificationMessageReceived(NotificationMessage obj)
{
if(obj.Notification == "LoginSuccess")
{
var test = Window.GetWindow(this);
test.Close();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.