在不同的 ViewModel 之间共享数据 [英] Sharing data between different ViewModels

查看:25
本文介绍了在不同的 ViewModel 之间共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个简单的 MVVM 项目,它有两个窗口:

I'm trying to develop an easy MVVM project that it has two windows:

  1. 第一个窗口是文本编辑器,我在其中绑定了一些属性,例如FontSizeBackgroundColor:

它的DataContextMainWindowViewModel:

public class MainWindowViewModel : BindableBase
{     
    public int EditorFontSize
    {
        get { return _editorFontSize; }
        set { SetProperty(ref _editorFontSize, value); }
    } 
.....

  1. 第二个窗口是选项窗口,我有一个用于更改字体大小的滑块:

<滑块最大值=30"最小值=10"值={Binding EditorFontSize }";></滑块>

它的DataContextOptionViewModel:

public class OptionViewModel: BindableBase
{     
    public int EditorFontSize
    {
        get { return _editorFontSize; }
        set { SetProperty(ref _editorFontSize, value); }
    }
.....

我的问题是我必须在选项窗口中获取滑块的值,然后我必须用这个值修改我的 TextBlock 的 FontSize 属性.但是我不知道如何将字体大小从 OptionViewModel 发送到 MainViewModel.

My problem is that I have to get the value of the slider in the option window and then I have to modify the FontSize property of my TextBlock with this value. But I don't know how to send the font size from OptionViewModel to MainViewModel.

我认为我应该使用:

  1. 共享模型
  2. MainWindowViewModel 中的模型和 OptionViewModel 中该模型的引用
  3. 其他系统,如通知、消息......

我希望你能帮助我.这是我的第一个 MVVM 项目,英语不是我的主要语言 :S

I hope that you can help me. It's my first MVVM project and English isn't my main language :S

谢谢

推荐答案

有很多方法可以在视图模型之间进行交流,很多点什么点是最好的.你可以看到它是如何完成的:

There are many ways to communicate between view models and a lot of points what the point is the best. You can see how it is done:

在棱镜中

作者:Caliburn

在我看来,最好的方法是使用 Prism 框架的 EventAggregator 模式.Prism 简化了 MVVM 模式.但是,如果您还没有使用过 Prism,您可以使用 Rachel Lim 的教程 - Rachel Lim 的 EventAggregator 模式的简化版本..我强烈推荐你使用 Rachel Lim 的方法.

In my view, the best approach is using EventAggregator pattern of Prism framework. The Prism simplifies MVVM pattern. However, if you have not used Prism, you can use Rachel Lim's tutorial - simplified version of EventAggregator pattern by Rachel Lim.. I highly recommend you Rachel Lim's approach.

如果您使用 Rachel Lim 的教程,那么您应该创建一个通用类:

If you use Rachel Lim's tutorial, then you should create a common class:

public static class EventSystem
{...Here Publish and Subscribe methods to event...}

并将事件发布到您的 OptionViewModel 中:

And publish an event into your OptionViewModel:

eventAggregator.GetEvent<ChangeStockEvent>().Publish(
new TickerSymbolSelectedMessage{ StockSymbol = "STOCK0" });

然后您在另一个MainViewModel构造函数中订阅一个事件:

then you subscribe in constructor of another your MainViewModel to an event:

eventAggregator.GetEvent<ChangeStockEvent>().Subscribe(ShowNews);

public void ShowNews(TickerSymbolSelectedMessage msg)
{
   // Handle Event
}

Rachel Lim 的简化方法是我见过的最好的方法.但是,如果你想创建一个大的应用程序,那么你应该阅读这个 文章 Magnus MontinCSharpcorner 示例.

The Rachel Lim's simplified approach is the best approach that I've ever seen. However, if you want to create a big application, then you should read this article by Magnus Montin and at CSharpcorner with an example.

更新:对于 Prism 5 以后的版本 CompositePresentationEvent 已弃用并在版本 6 中完全删除,因此您需要对其进行更改PubSubEvent 其他一切都可以保持不变.

Update: For versions of Prism later than 5 CompositePresentationEvent is depreciated and completely removed in version 6, so you will need to change it to PubSubEvent everything else can stay the same.

这篇关于在不同的 ViewModel 之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆