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

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

问题描述

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

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

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

<TextBlock FontSize="{Binding EditorFontSize}"></TextBlock>

DataContext MainWindowViewModel :

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

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

<Slider Maximum="30" Minimum="10" Value="{Binding EditorFontSize }" ></Slider>

DataContext OptionViewModel :

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模式.棱镜简化了MVVM模式. 但是,如果您尚未使用Prism,则可以使用

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:

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的简化方法是我见过的最好的方法.但是,如果您想创建一个大型应用程序,则应阅读此

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.

更新:对于版本低于5的Prism版本,在版本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天全站免登陆