我在MVVM架构中有一个WPF应用程序。 当应用程序加载时,我需要显示一个“登录”窗口,用户输入用户名和密码。
然后将其传递给ServiceLocator,后者创建连接到WCF服务的客户端。
问题:
如果客户端成功连接后如何关闭“登录”窗口,而不在View的'。cs'文件中使用任何代码隐藏?
答案 0 :(得分:1)
我通常在IDialogService实现中将ViewModel传递给Close Action。
public void ShowDialog(IDialogViewModel vm)
{
// create the dialog view
...
vm.CloseAction = () => dialog.Close();
dialog.DataContext = vm;
...
// show the dialog
dialog.ShowDialog();
}
答案 1 :(得分:0)
我使用dialogservice登录并公开活动
event EventHandler<RequestCloseEventArgs> RequestCloseDialog;
当客户端成功放入时关闭对话框。
编辑:这是一个例子
在app.xaml.cs
protected override void OnStartup(StartupEventArgs e)
{
var result = this._dialogService.Show("Login",this._myloginviewmodel);
// When use click cancel in the login dialog
if(!result)
{
// Close app or whatever
}
// Access the properties of myloginviewmodel if you want
// Now the only part of my app where I use view-first instead of viewmodel-first
this.MainWindow = new MainWindow(_mainwindowviwemodel);
this.MainWindow.Show();
}
这一切都在我的OnStartup()
中,是的,这就是我app.xaml
的代码隐藏,但这是我的应用程序根,这就是为什么这对我来说没问题。我没有AppViewModel或类似的东西,因为app.xaml
永远不会“显示”给用户。
答案 2 :(得分:0)
您可以覆盖Application类的OnStartup方法,您可以在其中打开Login屏幕作为对话框,一旦用户输入有效数据就可以返回布尔值,在登录屏幕关闭时,您可以根据布尔值做进一步的逻辑值。