在 Caliburn Micro 中使用 WindowManager 时如何从对话框返回对象

发布于 2024-12-13 02:01:11 字数 154 浏览 3 评论 0原文

我刚刚开始使用 Caliburn Micro WindowManager。

我已成功显示一个包含 WPF 视图的模式对话框。

我需要从对话框返回一个对象。最好的方法是什么?

背景 - 这是一个登录对话框。我需要验证用户名和密码并返回用户配置信息。

I'm just starting out with the Caliburn Micro WindowManager.

I have managed to display a modal dialog containing my WPF view.

I need to return an object from the dialog. What is the best way to do this?

Background - this is a login dialog. I need to validate the username and password and return user configuration information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏爱自由 2024-12-20 02:01:11

我可以想到两种方法来做到这一点。我假设您的登录视图模型类派生自 Caliburn.Micro.Screen。

1) 让视图模型(代表您的登录屏幕)公开一个返回用户配置的属性。然后,您可以在成功登录后访问它,如下所示:

var windowManager = new WindowManager();
var login = new MyLoginViewModel();
if (windowManager.ShowDialog(login) == true)
{
    var configurationInfo = login.MyUserConfigurationInfo;
}

2) 成功登录后将用户配置信息添加到应用程序的资源字典中。下面的示例显示您在登录视图模型内的“登录”方法中进行设置。

void Login()
{
    /* Authentication Logic success */
    App.Current.Resources["UserConfigurationInfo"] = new UserConfigurationInfo();
    TryClose(true);
}

There are two ways I can think of to do this. I'm assuming your Login viewmodel class derives from Caliburn.Micro.Screen.

1) Have the view model (representing your login screen) expose a property that returns the user configuration. You can then access it upon a successful login like below:

var windowManager = new WindowManager();
var login = new MyLoginViewModel();
if (windowManager.ShowDialog(login) == true)
{
    var configurationInfo = login.MyUserConfigurationInfo;
}

2) Add the user configuration information to the application's resource dictionary upon a successful login. The example below shows you setting this inside a "Login" method inside the login viewmodel.

void Login()
{
    /* Authentication Logic success */
    App.Current.Resources["UserConfigurationInfo"] = new UserConfigurationInfo();
    TryClose(true);
}
溺ぐ爱和你が 2024-12-20 02:01:11

我不知道 Caliburn Micro WindowManager 但如果您的模态对话框视图有一个视图模型,那么如何使用可以访问对象的视图模型呢?

在我的项目中,有一个 ILoginViewModel,我在 app.cs 中的 DialogResult=true 之后使用它。 (我使用 MEF 导出此信息,因此我的模块可以轻松访问)

i dont know Caliburn Micro WindowManager but if your modal dialog view has a viewmodel, what about to just take the viewmodel where you can access your object?

in my project a have a ILoginViewModel which i use after DialogResult=true in my app.cs. (i export this information with MEF, so its easy to access for my moduls)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文