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.

最新文章

  1. JavaScript中事件处理
  2. 便捷的方式在手机上查看Unity3D的Console Log(调试信息)
  3. js最佳继承范型
  4. SQL查询到的数据放到DataSet中
  5. 【突发问题】昨天更新了OS X EI Capitan 出现了Cocoapods的 pod :command not found
  6. 二、JavaScript语言--JS动画--JS动画效果
  7. BZOJ4491: 我也不知道题目名字是什么
  8. [LoadRunner]性能测试实践_Hessian协议脚本编写
  9. 错误解决error while loading shared libraries: libXXX.so.X: cannot open shared object file: No such file
  10. objective-c new关键字
  11. 解决Notice错误,性能竟然提升了1000多倍!
  12. Quartz表达式
  13. position containing block原点
  14. HDU 5536 Chip Factory 字典树+贪心
  15. html5 API
  16. 史上最全的css hack
  17. Node.js包管理器:
  18. 听翁恺老师mooc笔记(11)--结构和函数
  19. eclipse工具按键翻译
  20. 59A

热门文章

  1. Win10 UAP 绑定
  2. W:Failed to fetch http://archive.ubuntukylin.com:10006/ubuntukylin/dists/pre
  3. NS2中trace文件分析
  4. hdu 4759 大数+找规律 ***
  5. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并
  6. 关于MFC OpenGL环境配置的一点总结
  7. vs2015 MFC工程添加消息响应函数
  8. MySQL的多表查询(笛卡尔积原理)
  9. js Array对象
  10. mongodb学习05 操作详解(3)