Silverlight应用程序中MVVM模式入门指南【翻译】
2011-07-18 23:35
686 查看
作为一个应用程序开发框架,silverlight越来越流行,有关设计模式的讨论的呼声也越来越高,幸运的是,在silverlight的世界中,大部分silverlight开发人员都逐渐认可Model-View-ViewModel (MVVM)模式,MVVM模式把应用程序分成几个独立的层次,这样的做法有许多好处:更好的代码重用、增强测试功能,本文章将解释MVVM中的关键概念,并且以一种简单容易理解的方式来介绍展示。我同时也会写一些代码来解释MVVM怎么使用,代码会在文章的稍后部分展示,也可以在此下载。
MVVM模式入门 MVVM会定义三部分,包括Model、View和ViewModel,下图中展示图片是来自我们silverlight课程中的幻灯片,以一种简明的方式总结了MVVM模式中各个部分。
<UserControl x:Class="ViewModelExample.MainPage"
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"
xmlns:converter="clr-namespace:ViewModelExample"
xmlns:viewModel="clr-namespace:ViewModelExample.ViewModel"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<UserControl.Resources>
<viewModel:PeopleViewModel x:Key="ViewModel" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource ViewModel}}">
</Grid>
</UserControl>[/code]
ViewModel命名空间使用ViewModel作为XML命名空间的前缀,然后ViewModel使用关键字ViewModel定义在<UserControl.Resources>中,关键字是非常重要的,因为它“劫持”ViewModel至DataContext,layout的子元素可以绑定在ViewModel上,下面是绑定ListBox、StackPanel至ViewModel的People属性。
MVVM模式提供了灵活的方式来处理数据,增加代码重用、简化、易维护。当然MVVM还有更多可讨论的地方,例如:event buses、commanding、dependency injection,但我希望这篇文章可以帮助你开发silverlight应用程序。
译自:http://weblogs.asp.net/dwahlin/archive/2009/12/08/getting-started-with-the-mvvm-pattern-in-silverlight-applications.aspx
由于本人翻译水平有限,有些地方欠妥,请园友们不吝指教!
MVVM模式入门 MVVM会定义三部分,包括Model、View和ViewModel,下图中展示图片是来自我们silverlight课程中的幻灯片,以一种简明的方式总结了MVVM模式中各个部分。
<UserControl x:Class="ViewModelExample.MainPage"
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"
xmlns:converter="clr-namespace:ViewModelExample"
xmlns:viewModel="clr-namespace:ViewModelExample.ViewModel"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<UserControl.Resources>
<viewModel:PeopleViewModel x:Key="ViewModel" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource ViewModel}}">
</Grid>
</UserControl>[/code]
ViewModel命名空间使用ViewModel作为XML命名空间的前缀,然后ViewModel使用关键字ViewModel定义在<UserControl.Resources>中,关键字是非常重要的,因为它“劫持”ViewModel至DataContext,layout的子元素可以绑定在ViewModel上,下面是绑定ListBox、StackPanel至ViewModel的People属性。
<StackPanel Margin="20"> <TextBlock Text="Binding Controls to a ViewModel" Margin="20,0,0,0" FontWeight="Bold" FontSize="12" /> <ListBox x:Name="lbPeople" Margin="0,10,0,0" Height="250" Width="300" HorizontalAlignment="Left" ItemsSource="{Binding People}" ScrollViewer.HorizontalScrollBarVisibility="Hidden" SelectedItem="{Binding Person, Mode=TwoWay}"> <ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" /> <ColumnDefinition Width="100" /> <ColumnDefinition Width="100" /> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Margin="10" Text="{Binding FirstName}" /> <TextBlock Grid.Column="1" Margin="10" Text="{Binding LastName}" /> <TextBlock Grid.Column="2" Margin="10" Text="{Binding Age}" /> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <StackPanel DataContext="{Binding Person}"> <TextBlock Text="First Name" Margin="0,10,0,0" /> <TextBox Text="{Binding FirstName,Mode=TwoWay}" Width="100" Height="25" HorizontalAlignment="Left"/> <TextBlock Text="Last Name" /> <TextBox Text="{Binding LastName,Mode=TwoWay}" Width="100" Height="25" HorizontalAlignment="Left"/> <TextBlock Text="Age" /> <TextBox Text="{Binding Age,Mode=TwoWay}" Width="100" Height="25" HorizontalAlignment="Left"/> </StackPanel> <Button Margin="0,10,0,0" Click="Button_Click" Content="Submit" Height="20" Width="100" HorizontalAlignment="Left" /> </StackPanel>
MVVM模式提供了灵活的方式来处理数据,增加代码重用、简化、易维护。当然MVVM还有更多可讨论的地方,例如:event buses、commanding、dependency injection,但我希望这篇文章可以帮助你开发silverlight应用程序。
译自:http://weblogs.asp.net/dwahlin/archive/2009/12/08/getting-started-with-the-mvvm-pattern-in-silverlight-applications.aspx
由于本人翻译水平有限,有些地方欠妥,请园友们不吝指教!
相关文章推荐
- [Silverlight入门系列]使用MVVM模式(4):Prism的NotificationObject自动实现INotifyPropertyChanged接口
- [Silverlight入门系列]Prism中TreeView真正实现MVVM模式和Expanded发生时异步动态加载子节点(WCFRiaService)
- [Silverlight入门系列]使用MVVM模式(5):异步Validation数据验证和INotifyDataErrorInfo接口
- [Silverlight入门系列]使用MVVM模式(9): 想在ViewModel中控制Storyboard动画?
- [Silverlight入门系列]使用MVVM模式(1):MVVM核心概念
- [Silverlight入门系列]使用MVVM模式(6):使用Behavior
- [Silverlight入门系列]使用MVVM模式(7):ViewModel的INotifyPropertyChanged接口实现
- Silverlight商业应用程序开发学习笔记(12) MVVM设计模式相关--
- [Silverlight入门系列]使用MVVM模式(4):Prism的
- [Silverlight入门系列]使用MVVM模式(8):ViewModel进一步分离出Services
- Silverlight入门系列]使用MVVM模式
- 【翻译】使用MVVM模式构建WPF应用程序介绍
- [Silverlight入门系列]使用MVVM模式(9): 想在ViewModel中控制Storyboard动画?
- Silverlight/WPF/Windows Phone 开发之MVVM设计模式之入门
- MVVM设计模式相关--Silverlight商业应用程序开发学习笔记(12)
- [Silverlight入门系列]使用MVVM模式(1):MVVM核心概念
- [Silverlight入门系列]使用MVVM模式(2):集合Model /ObservableCollection/ICollectionView
- 转:[Silverlight入门系列]使用MVVM模式(9): 想在ViewModel中控制TreeView节点展开?
- 摘记[理论]原创翻译:基于MVVM设计模式的WPF应用程序
- Gradle2.0用户指南翻译——第十章. Web 应用程序快速入门