MVVM儿童模式和导航和啤酒 [英] MVVM Child models and Navigation and beer

查看:242
本文介绍了MVVM儿童模式和导航和啤酒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是在酒吧和我有几瓶啤酒,这丫头是主张让MVVM与现实世界的应用程序的工作是痛苦,她说,为了解决MVVM问题,你添加更多的代码,那么你再弄问题和你添加更多的代码,它永远不会结束,我同意,当我读到MVVM这听起来不错,我不想用棱镜,也不MVVM光,我只是需要一种方法来我的视图之间切换。和我的应用程序看起来像这样所以请激励我之前,我酒精中毒

i was at the bar and i had couple beers, and this girl was arguing that getting mvvm to work with a real world applications is pain, she said that in order to solve problem in mvvm you add more code then you get another problem and the you add more code and it never ends, and i agree, when i read about mvvm it sounded nice, and i don't want to use prism nor MVVM light, i just need a way to switch between my views. and my application looks like this so please inspire me before i get alcohol poisoning

在这里输入的形象描述

推荐答案

如果你想要松散耦合的通信需要一个EventAggregator:

If you want loosely-coupled communication you need an EventAggregator:

//Simplest EventAggregator
public static class DumbAggregator
{
    public static void BroadCast(string message)
    {
       if (OnMessageTransmitted != null)
           OnMessageTransmitted(message);
    }

    public static Action<string> OnMessageTransmitted;
}



用法:

Usage:

public class MySender
{
   public void SendMessage()
   {
       DumbAggregator.BroadCast("Hello There!");
   }
}

public class MySubscriber
{
   public MySubscriber()
   {
       DumbAggregator.OnMessageTransmitted += OnMessageReceived;
   }

   private void OnMessageReceived(string message)
   {
      MessageBox.Show("I Received a Message! - " + message);
   }
}



如果你不喜欢孩子的ViewModels,你可以把一切的单一视图模型里面,有一些 DataTriggers 或一些动态改变的观点,然而父母 - 儿童视图模型的方法是干净多了IMO

And if you don't like Child ViewModels, you can put everything inside a single ViewModel, and have some DataTriggers or something to dynamically change views, however a Parent - Children ViewModel approach is much cleaner IMO.

这篇关于MVVM儿童模式和导航和啤酒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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