如何从多个视图模型更新状态栏?

发布于 2024-09-26 06:16:39 字数 63 浏览 1 评论 0原文

我有一个带有状态栏和多个用户控件的主窗口。每个用户控件都有一个视图模型。如何从多个视图模型绑定/更新状态栏文本?

I have MainWindow with a statusbar and multiple user controls in it. Each user control has a viewmodel. How do I bind/update the statusbar text from multiple viewmodels?

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

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

发布评论

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

评论(2

或十年 2024-10-03 06:16:39

这个很简单,我自己也用过。

实现 Messenger系统并给它一个 Singleton 实例,例如... StatusBarMessenger

MainWindow View-Model:

  • 需要一个字符串属性,例如。地位。
  • 需要注册到Ctor中的StatusBarMessenger
StatusBarMessenger.Instance.Register(this, new Action(s => Status = s));

所有需要向状态栏发送文本的VM可能会执行以下操作:

StatusBarMessenger.Instance.Send("Hello world");

This is a simple one that I have used myself.

Implement a Messenger system and give it a Singleton instance, say... StatusBarMessenger

MainWindow View-Model:

  • Needs a string property, eg. Status.
  • Needs to register to the StatusBarMessenger in the Ctor
StatusBarMessenger.Instance.Register(this, new Action(s => Status = s));

All VM that requires to send text to the status bar may do the following:

StatusBarMessenger.Instance.Send("Hello world");
悍妇囚夫 2024-10-03 06:16:39

如果您使用 PRISM,则可以使用其 EventAggregator 发布(从用户控件)消息并在 StatusBar VM 中订阅这些消息。

If you are using PRISM then you can use its EventAggregator to Publish (from your user controls) messages and in StatusBar VM Subscribe to those messages.

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