MVVM模式与Reactive Extensions 学习与思考
MVVM来历:John Gossman 于2005写了一篇关于Model-View-ViewModel模式的博文,这种模式被他所在的微软的项目组用来创建Expression Blend(即'Sparkle')。它跟Martin Fowler的Presentation Model非常相似,唯一不同的是,它填平了presentation model和使用了WPF的丰富的数据绑定的view之间的沟壑。在Dan Crevier发表了神作DataModel-View-ViewModel series博文系列之后,(D)MVVM模式开始变得流行起来。
现目前的MVVM的小问题:从现目前的MVVM大多数都是通过如:
代码
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras"
x:Class="MvvmLightDragAndDrop.MainPage"
Height="300"
Width="300">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<UserControl.DataContext>
<Binding Path="Main"
Source="{StaticResource Locator}" />
</UserControl.DataContext>
<Grid x:Name="LayoutRoot"
AllowDrop="True"
Background="#FF9F9F9F">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Drop">
<cmd:EventToCommand Command="{Binding HandleDropCommand, Mode=OneWay}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
<TextBlock FontSize="36"
FontWeight="Bold"
Foreground="Purple"
Text="{Binding DroppedFileContent}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
TextWrapping="Wrap"
TextTrimming="WordEllipsis" />
</Grid>
</UserControl>
绑定控件的ICommand属性。它使得事件相当的静态耦合,这样的方式灵活性扩展性似乎不是那么理想,让人感觉有点别扭。
Reactive Extensions出来后对MVVM又有了一些新的实现方法: ReactiveXaml
思考:能不能像JQuery那样来处理事件及逻辑呢?


浙公网安备 33010602011771号