在 MVVMLight 中设置 ViewModel 的 DataContext

发布于 2024-12-06 18:40:25 字数 695 浏览 0 评论 0原文

我有一个关于 MVVM 模式的问题。所以我不确定我是否完全理解了它。

我的场景:

  • ViewModelLocator :将请求的 ViewModel 提供给指定的视图。
  • LoginViewModel:LoginView 的 ViewModel
  • MainPageViewModel:MainPageView 的 ViewModel

我的示例应用程序。很简单:用户可以登录并进入 MainPageView。

MainPageView 使用 MainPageViewModel。我使用 MVVMLight 框架的信使从 LoginView 导航到 MainPageView。

 Messenger.Default.Register<LoginPerson>(this, Constants.NavigateLogin,
                person => this.Window.SetContentControl(new MainPage(person)));

我将登录者传递给视图。 MainPage - View 会将登录者设置为其 ViewModel (=> MainPageViewModel)。

这种方式正确吗?我不这么认为:-) 如何在 ViewModel 之间进行通信?感谢您的建议。

问候,亲

I've got a question of the MVVM-Pattern. So I'm not sure, that I've had understand it completely.

My scenario:

  • ViewModelLocator : Provide the requested ViewModel to a specified view.
  • LoginViewModel: ViewModel for the LoginView
  • MainPageViewModel: ViewModel for the MainPageView

My example app. is simple: The user can login and comes to the MainPageView.

The MainPageView uses the MainPageViewModel. I use the messenger of the MVVMLight framework to navigate from the LoginView to the MainPageView.

 Messenger.Default.Register<LoginPerson>(this, Constants.NavigateLogin,
                person => this.Window.SetContentControl(new MainPage(person)));

I pass the loggedin person to the View. The MainPage - View will set the the logged in person to its ViewModel (=> MainPageViewModel).

Is this way correct? I don't think so :-) How can I communicate between the ViewModels? Thanks for your advices.

Regards, pro

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

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

发布评论

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

评论(1

早茶月光 2024-12-13 18:40:25

使用 MVVM 时,您的应用程序是 ViewModel,而不是 View。您不应该处理任何类型的业务逻辑,例如导航或从视图传递用户对象。 View 只是一个漂亮的层,允许用户轻松地与 ViewModel 进行交互。

通常在这种情况下,我使用 ShellViewModel ,其中包含一个 CurrentPage 属性,该属性设置为当前页面的任何 ViewModel。我还将在 ShellViewModel 中存储 CurrentUser 属性。

您的 ShellViewModel 是您的启动对象,启动时 CurrentPage 将是 LoginViewModel。当用户成功登录时,LoginViewModel 广播一条 LoginSuccessful 消息,参数为 CurrentUserShellViewModel > 将拾取该消息并根据消息参数设置 CurrentUser,并将 CurrentView 切换到新的 MainPageViewModel

例如,请查看我的帖子这里

When using MVVM, your application is your ViewModels, not your Views. You should not be handling any kind of business logic such as navigation or passing User objects around from your Views. The View is simply a pretty layer that allows the user's to interact with your ViewModels easily.

Usually in this kind of situation I use a ShellViewModel which contains a CurrentPage property that is set to whatever ViewModel is the CurrentPage. I would store a CurrentUser property in the ShellViewModel as well.

Your ShellViewModel is your startup object, and on startup the CurrentPage would be a LoginViewModel. When the user successfully logs in, the LoginViewModel broadcasts a LoginSuccessful message with a parameter of the CurrentUser, and the ShellViewModel would pickup that message and set the CurrentUser based on the message parameters, and would switch CurrentView to a new MainPageViewModel

For an example, check out my post here

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