Basically there are two possible ways to send a message, first using an overload of the RaisePropertyChanged and secondly by using the Messenger.Default.Send command. For the address detail screen I used the messaging overload of RaisePropertyChanged.

public AddressViewModel SelectedAddress
{
set
{
var oldValue = selectedAddress;
selectedAddress = value;
this.RaisePropertyChanged(() => SelectedAddress, oldValue, selectedAddress, true);
NavigationService.Navigate("/Views/AddressDetailView.xaml");
}
get
{
return selectedAddress;
}
}

The RaisePropertyChanged method includes an overload that accepts a boolean argument, called broadcast. This last argument of the RaisePropertyChanged event indicates whether or not a message should be broadcasted regarding this change. MVVM Light, under the covers construct and sends a message of typePropertyChanged<AddressViewModel>. So any subscribers listening for this specific message will get it delivered via the messaging infrastructure.

public AddressDetailViewModel(IAddressRepository addressRepository,
IRideRepository rideRepository)
{
this.addressRepository = addressRepository;
this.rideRepository = rideRepository; SaveAddressCommand = new RelayCommand(SaveAddress);
CancelCommand = new RelayCommand(Cancel);
DeleteCommand = new RelayCommand(Delete); Messenger.Default.Register<PropertyChangedMessage<AddressViewModel>>(
this,
message =>
{
SelectedAddress = null;
SelectedAddress = message.NewValue;
if (message.NewValue != null)
{
whichAddress = message.NewValue.WhichAddress;
}
});
}

The entire AddressViewModel is sent as payload with the message and is directly set to the SelectedAddressproperty of the AddressDetailViewModel. This property is bound to the different fields on the view which in its turn gets refreshed so that the data is directly shown on the screen.

By communicating using messages instead of direct references, you decouple both view models.This will increase the flexibility and testability of your view models.

MVVMLight Messenging的更多相关文章

  1. UWP开发之Mvvmlight实践九:基于MVVM的项目架构分享

    在前几章介绍了不少MVVM以及Mvvmlight实例,那实际企业开发中将以那种架构开发比较好?怎样分层开发才能节省成本? 本文特别分享实际企业项目开发中使用过的项目架构,欢迎参照使用!有不好的地方欢迎 ...

  2. UWP开发之Mvvmlight实践八:为什么事件注销处理要写在OnNavigatingFrom中

    前一段开发UWP应用的时候因为系统返回按钮事件(SystemNavigationManager.GetForCurrentView().BackRequested)浪费了不少时间.现象就是在手机版的详 ...

  3. UWP开发之Mvvmlight实践七:如何查找设备(Mobile模拟器、实体手机、PC)中应用的Log等文件

    在开发中或者后期测试乃至最后交付使用的时候,如果应用出问题了我们一般的做法就是查看Log文件.上章也提到了查看Log文件,这章重点讲解下如何查看Log文件?如何找到我们需要的Packages安装包目录 ...

  4. [原创]MvvmLight中用IDialogService替代DialogMessage的用法

    在新版的MvvmLight中,DialogMessage被标注为已过时,需要用IDialogService来替代,IDialogService的具体用法如下: 先在主窗体中实现IDialogServi ...

  5. UWP开发之Mvvmlight实践二:Mvvmlight的核心框架MVVM与MVC、MVP的区别(图文详解)

    最近UWP开发在海外很潮流,随着微软收购Xamarin,我们这些C#程序员也可以靠这杆小米枪挑战Android,IOS平台了. 那我们为什么选择MVVM做UWP开发?MVC,MVP,MVVM他们之间到 ...

  6. UWP开发之Mvvmlight实践一:如何在项目中添加使用Mvvmlight(图文详解)

    最近一直在做UWP开发,为了节省成本等等接触到MVVMlight,觉得有必要发点时间研究它的用法与实现原理才行.如果有问题的地方或者有好的建议欢迎提出来. 随着移动开发的热门,Mvvmlight在An ...

  7. MvvmLight ToolKit .Net4.5版本 CanExecute不能刷新界面bug

    一 问题重现    1.在使用最新版本v5.1的MvvmLight中(其实这个问题很早就有了),发现CanExecute不能很好地工作了.一个简单的工程,只有MainWindow和MainWindow ...

  8. 【MVVMLight小记】二.开发一个简单图表生成程序附源码

    上一篇文章介绍了怎样快速搭建一个基于MVVMLight的程序http://www.cnblogs.com/whosedream/p/mvvmlight1.html算是简单入门了下,今天我们来做一个稍许 ...

  9. 【MVVMLight小记】一.快速搭建一个基于MVVMLight的silverlight小程序

    写了篇MVVM小记http://www.cnblogs.com/whosedream/p/mvvmnote1.html,说好要写点MVVMLight的东西,所以接着写,以便和大家共勉. 我假设你已经有 ...

随机推荐

  1. AndroidStudio创建新项目报错

    创建新项目自动执行时报错: Failed to import new Gradle project: failed to find Build Tools revision 17.0.0 Consul ...

  2. 函数式编程语言LISP,python,haskell,clojure

    说说我自己的背景吧,我是个半吊子的程序员,做任何事情喜欢比较了解然后再尝试,我接触过很多语言,大多数都把它当成工具来使用 我现在的工作大部分主要在于数据挖掘与机器学习方面,也学习web开发,我第一个拿 ...

  3. Alcatraz安装 不能用解决方案

    1.安装 1>Github上下载Alcatraz,下载地址:https://github.com/supermarin/Alcatraz  2>Alcatraz是xcode的插件,这个插件 ...

  4. C++异常处理机制几种方法

    一.异常 迄今为止,我们处理程序中的错误一般都是用if语句测试某个表达式,然后处理错误的特定义代码. C++异常机制使用了三个新的关键字  (SEH(结构化异常处理)) try    ──标识可能出现 ...

  5. Loadrunner中动态添加虚拟用户

    添加的虚拟用户会立马开始执行: 场景组的两种模式: Vuser组模式: Vuser组模式中,对于压力负载机是不能同时添加多个 但是我们可以在一个脚本里通过多次添加Vuser,每次添加30个Vuser, ...

  6. lr中定义字符串变量

    需要注意的是数组必须定义为固定的长度,如:char chary[20]: 5C2o"Go!\gm  nHH0 数组的最大长度为32064(32K),否则会出现“too many variab ...

  7. 在WebBrowser中通过模拟键盘鼠标操控网页中的文件上传控件(转)

    引言 这两天沉迷了Google SketchUp,刚刚玩够,一时兴起,研究了一下WebBrowser. 我在<WebBrowser控件使用技巧分享>一文中曾谈到过“我现在可以通过WebBr ...

  8. 一个linux的样本分析

    不久前收到的一个linux样本,之前linux平台下的样本见得并不多,正好做个记录. 样本启动之后,会将自身重命名拷贝到/usr/bin下,并删除自身,如此处就将自身文件amdhzbenfi命名为us ...

  9. json 入门(1)

    1.JSONObject介绍 JSONObject-lib包是一个beans,collections,maps,Java arrays和xml和JSON互相转换的包. 2.下载jar包 http:// ...

  10. Ipython console in Spyder stuck on “connecting to kernel”

    简短地记录下,今天排除的spyder的BUG, 现象:打开Spyder时其他正常,但是Ipython console 不能正常获取到kernel,一直转圈,显示“connecting to kerne ...