Silverligth MVVM 学习与应用
支持模型-视图-视图模型 (MVVM) 模式是独立从基础业务逻辑的用户界面应用程序的模式.
该模式可以使用到 WPF,Silverligth,Windows phone 应用 。
Model:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace SLMvvmDemo2.Model { //Model 模型 //客户表 public class Customer { //编号 public string CustomerID { get; set; } //名称 public string CustomerFullName { get; set; } //电话号码 public string CustomerPhone { get; set; } //地址 public string CustomerAddress { get; set; } //备注 public string CustomerRemark { get; set; } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System.Collections.Generic; using System.Linq; namespace SLMvvmDemo2.Model { // 检索和更新 可移植类库 项目中的数据。 // 在实际应用程序中,您将检索源数据, // 如 Windows Communication Foundation (WCF) 服务。 public class CustomerService { private List<Customer> _customer; public CustomerService() { _customer=new List<Customer>() { new Customer(){CustomerAddress = "四川成都高新区",CustomerFullName = "张强",CustomerID = "000001",CustomerPhone = "18655228854",CustomerRemark = ""}, new Customer(){CustomerAddress = "四川成都高新区",CustomerFullName = "赵大",CustomerID = "000002",CustomerPhone = "082-5554523",CustomerRemark = ""}, new Customer(){CustomerAddress = "四川成都高新区",CustomerFullName = "刘一",CustomerID = "000003",CustomerPhone = "028-5584224",CustomerRemark = ""} }; } //获取所有 public List<Customer> GetAllCustomer() { return _customer; } //更新 public void UpdateCustomer(Customer customer) { Customer cusomerchange = _customer.Single(o => o.CustomerID == customer.CustomerID); cusomerchange = customer; } } }
ViewModel:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.ComponentModel; using System.Windows.Input; namespace SLMvvmDemo2.ViewModel { //视图模型的基类 public class ViewModelBase :INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public virtual void OnPropertyChanged(string propName) { if (PropertyChanged != null) { PropertyChanged(this,new PropertyChangedEventArgs(propName)); } } } //ICommand 接口的实现 public class RelayCommand : ICommand { private readonly Action _handler; private bool _isEnabled; //是否启用 public bool IsEnabled { get { return _isEnabled; } set { if (value != _isEnabled) { _isEnabled = value; if (CanExecuteChanged != null) { CanExecuteChanged(this, EventArgs.Empty); } } } } public RelayCommand(Action handler) { _handler = handler; } public bool CanExecute(object parameter) { return IsEnabled; } public event EventHandler CanExecuteChanged; public void Execute(object parameter) { _handler(); } } } using System.Collections.Generic; using SLMvvmDemo2.Model; namespace SLMvvmDemo2.ViewModel { public class SlCommonInfoViewModel:ViewModelBase { private List<Model.Customer> _customers; private Model.Customer _currentCustomer; private readonly Model.CustomerService _repository; public SlCommonInfoViewModel() { _repository=new CustomerService(); _customers = _repository.GetAllCustomer(); WiteCommands(); } private void WiteCommands() { UpdateCustomerCommand=new RelayCommand(UpdateCustomer); } public RelayCommand UpdateCustomerCommand { get; private set; } public List<Customer> Customers { get; set; } public Customer CurrentCustomer { get { return _currentCustomer; } set { if (_currentCustomer != value) { _currentCustomer = value; OnPropertyChanged("CurrentCustomer"); UpdateCustomerCommand.IsEnabled = true; } } } public void UpdateCustomer() { _repository.UpdateCustomer(CurrentCustomer); } } }
VIew:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<controls:ChildWindow x:Class="SLMvvmDemo2.Views.SLCommonInfo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" xmlns:controls2="clr-namespace:SLMvvmDemo2.Controls" Width="782" Height="319" Style="{StaticResource ChildWindowStyle}" Title="客户基本信息"> <Grid> <controls2:TiledBackground SourceUri="/SLMvvmDemo2;component/Images/backgroundtexture.png" /> <Image Source="/SLMvvmDemo2;component/Images/backgroundshadow.png" Stretch="Fill" /> <Border x:Name="BackgroundBorder" Style="{StaticResource BackgroundBorderStyle}" /> <Grid x:Name="LayoutRoot" Margin="2" Style="{StaticResource LayoutRootGridStyle}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="141*" /> <ColumnDefinition Width="617*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="33*" /> <RowDefinition Height="33*" /> <RowDefinition Height="33*" /> <RowDefinition Height="33*" /> <RowDefinition Height="120*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <TextBlock Height="23" Style="{StaticResource ContentTextStyle}" HorizontalAlignment="Right" Name="textBlock1" Text="客户编号:" VerticalAlignment="Center" /> <TextBlock Grid.Row="1" Style="{StaticResource ContentTextStyle}" Height="23" HorizontalAlignment="Right" Name="textBlock2" Text="客户名称:" VerticalAlignment="Center" /> <TextBlock Grid.Row="2" Style="{StaticResource ContentTextStyle}" Height="23" HorizontalAlignment="Right" Name="textBlock3" Text="电话号码:" VerticalAlignment="Center" /> <TextBlock Grid.Row="3" Style="{StaticResource ContentTextStyle}" Height="23" HorizontalAlignment="Right" Name="textBlock4" Text="客户地址:" VerticalAlignment="Center" /> <TextBlock Grid.Row="4" Style="{StaticResource ContentTextStyle}" Height="23" HorizontalAlignment="Right" Name="textBlock5" Text="备注:" VerticalAlignment="Center" /> <ComboBox Grid.Column="1" Height="25" HorizontalAlignment="Left" Name="txtCustomerID" VerticalAlignment="Center" Width="205" Margin="0,1" DisplayMemberPath="CustomerFullName" SelectedItem="{Binding Path=CurrentCustomer,Mode=TwoWay}" ItemsSource="{Binding Path=Customers}" /> <TextBox Height="25" HorizontalAlignment="Left" Name="txtCusomerName" VerticalAlignment="Center" Width="348" Grid.Column="1" Text="{Binding Path=CurrentCustomer.CustomerFullName,Mode=TwoWay}" Grid.Row="1" /> <TextBox Height="25" HorizontalAlignment="Left" Name="txtCustomerPhone" VerticalAlignment="Center" Width="348" Grid.Column="1" Text="{Binding Path=CurrentCustomer.CustomerPhone,Mode=TwoWay}" Grid.Row="2" /> <TextBox Height="25" HorizontalAlignment="Left" Name="txtCustomerAddress" VerticalAlignment="Center" Width="348" Grid.Column="1" Text="{Binding Path=CurrentCustomer.CustomerAddress,Mode=TwoWay}" Grid.Row="3" /> <TextBox Height="120" HorizontalAlignment="Left" Name="txtCustomerRemark" VerticalAlignment="Center" Width="617" Grid.Column="1" Text="{Binding Path=CurrentCustomer.CustomerRemark,Mode=TwoWay}" Grid.Row="4" /> <Button x:Name="CancelButton" Content="取消" Click="CancelButton_Click" Width="75" FontWeight="Bold" Height="35" HorizontalAlignment="Right" Grid.Row="5" Grid.Column="1" /> <Button x:Name="OKButton" Content="确定" Command="{Binding UpdateCustomerCommand}" Width="75" Height="35" FontWeight="Bold" HorizontalAlignment="Right" Margin="0,0,84,0" Grid.Row="5" Grid.Column="1" /> </Grid> </Grid> </controls:ChildWindow>