简练的视图模型 ViewModel

时间:2022-04-22
本文章向大家介绍简练的视图模型 ViewModel,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

patterns & practices Developer Center 发布了 Unity Application Block 1.2 for Silverlight - December 2008  依赖注入容器。用其来做MVP模式的silverlight会非常的方便,在正式开始MVP模型的学习前先简单的做了一个视图模型ViewModel的演练。

这里是我的代码:

ChristmasModel.cs
 using System;
 using System.ComponentModel;
 
 namespace ChristmasViewModel
 {
 public class ChristmasModel : INotifyPropertyChanged
     {
 private bool _isShow1 = true;
 private bool _isShow2 = true;
 private bool _isShow3 = true;
 private bool _isShow4 = true;
 private bool _isShow5 = true;
 private bool _isShow6 = true;
 private bool _isShow7 = true;
 private bool _isShow8 = true;
 private bool _isShow9 = true;
 private bool _isShow10 = true;
 
 public bool IsShow1
         {
 get { return this._isShow1; }
 set {
 if (this._isShow1 != value) {
 this._isShow1 = value;
 this.RaisePropertyChanged("IsShow1");
                 }
             }
         }
 public bool IsShow2
         {
 get { return this._isShow2; }
 set
             {
 if (this._isShow2 != value)
                 {
 this._isShow2 = value;
 this.RaisePropertyChanged("IsShow2");
                 }
             }
         }
 public bool IsShow3
         {
 get { return this._isShow3; }
 set
             {
 if (this._isShow3 != value)
                 {
 this._isShow3 = value;
 this.RaisePropertyChanged("IsShow3");
                 }
             }
         }
 public bool IsShow4
         {
 get { return this._isShow4; }
 set
             {
 if (this._isShow4 != value)
                 {
 this._isShow4 = value;
 this.RaisePropertyChanged("IsShow4");
                 }
             }
         }
 public bool IsShow5
         {
 get { return this._isShow5; }
 set
             {
 if (this._isShow5 != value)
                 {
 this._isShow5 = value;
 this.RaisePropertyChanged("IsShow5");
                 }
             }
         }
 public bool IsShow6
         {
 get { return this._isShow6; }
 set
             {
 if (this._isShow6 != value)
                 {
 this._isShow6 = value;
 this.RaisePropertyChanged("IsShow6");
                 }
             }
         }
 public bool IsShow7
         {
 get { return this._isShow7; }
 set
             {
 if (this._isShow7 != value)
                 {
 this._isShow7 = value;
 this.RaisePropertyChanged("IsShow7");
                 }
             }
         }
 public bool IsShow8
         {
 get { return this._isShow8; }
 set
             {
 if (this._isShow8 != value)
                 {
 this._isShow8 = value;
 this.RaisePropertyChanged("IsShow8");
                 }
             }
         }
 public bool IsShow9
         {
 get { return this._isShow9; }
 set
             {
 if (this._isShow9 != value)
                 {
 this._isShow9 = value;
 this.RaisePropertyChanged("IsShow9");
                 }
             }
         }
 public bool IsShow10
         {
 get { return this._isShow10; }
 set
             {
 if (this._isShow10 != value)
                 {
 this._isShow10 = value;
 this.RaisePropertyChanged("IsShow10");
                 }
             }
         }
 
 #region INotifyPropertyChanged Members
 
 public event PropertyChangedEventHandler PropertyChanged;
 
 
 private void RaisePropertyChanged(string propertyName) {
 if (this.PropertyChanged != null) {
 this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
             }
         }
 #endregion
     }
 }
 
VisibilityConverter.cs
 using System;
 using System.Windows;
 
 namespace ChristmasViewModel
 {
 public class VisibilityConverter : System.Windows.Data.IValueConverter
     {
 
 #region IValueConverter Members
 
 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
         {
 bool isVisible = (bool)value;
 return (isVisible ? Visibility.Visible : Visibility.Collapsed);
         }
 
 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
         {
 bool isVisible = ((Visibility)value == Visibility.Visible);
 return isVisible;
         }
 
 #endregion
     }
 }
 
page.xaml
 <UserControl x:Class="ChristmasViewModel.Page"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Width="338" Height="176" xmlns:ChristmasViewModel="clr-namespace:ChristmasViewModel" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
 <UserControl.Resources>
 <ChristmasViewModel:VisibilityConverter x:Key="VisibilityConverterDS" d:IsDataSource="True"/>
 </UserControl.Resources>
 <UserControl.DataContext>
 <ChristmasViewModel:ChristmasModel/>
 </UserControl.DataContext>
 <Grid x:Name="LayoutRoot" Background="White">
 <StackPanel Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto">
 <StackPanel Height="84" Width="Auto" Orientation="Horizontal">
 <StackPanel Height="Auto" Width="Auto">
 <Image Source="assets/01_User.png" Stretch="Fill" Height="64" Width="64" Visibility="{Binding Converter={StaticResource VisibilityConverterDS}, Path=IsShow1}"/>
 <CheckBox Content="Show" Width="64" Height="17" IsChecked="{Binding Mode=TwoWay, Path=IsShow1}"/>
 </StackPanel>
 <StackPanel Height="Auto" Width="Auto">
 <Image Source="assets/02_ShoppingCart.png" Stretch="Fill" Visibility="{Binding Converter={StaticResource VisibilityConverterDS}, Path=IsShow2}" Height="64" Width="64"/>
 <CheckBox Width="64" Content="Show" Height="20" IsChecked="{Binding Mode=TwoWay, Path=IsShow2}"/>
 </StackPanel>
 <StackPanel Height="Auto" Width="Auto">
 <Image Source="assets/03_RSS.png" Stretch="Fill" Visibility="{Binding Converter={StaticResource VisibilityConverterDS}, Path=IsShow3}" Height="64" Width="64"/>
 <CheckBox Width="64" Content="Show" Height="20" IsChecked="{Binding Mode=TwoWay, Path=IsShow3}"/>
 </StackPanel>
 <StackPanel Height="Auto" Width="Auto">
 <Image Source="assets/04_Portfolio.png" Stretch="Fill" Visibility="{Binding Converter={StaticResource VisibilityConverterDS}, Path=IsShow4}" Height="64" Width="64"/>
 <CheckBox Width="64" Content="Show" Height="20" IsChecked="{Binding Mode=TwoWay, Path=IsShow4}"/>
 </StackPanel>
 <StackPanel Height="Auto" Width="Auto">
 <Image Source="assets/05_Contact.png" Stretch="Fill" Visibility="{Binding Converter={StaticResource VisibilityConverterDS}, Path=IsShow5}" Height="64" Width="64"/>
 <CheckBox Width="64" Content="Show" Height="20" IsChecked="{Binding Mode=TwoWay, Path=IsShow5}"/>
 </StackPanel>
 </StackPanel>
 <StackPanel Height="84" Orientation="Horizontal">
 <StackPanel Height="Auto" Width="Auto">
 <Image Source="assets/06_Comment.png" Stretch="Fill" Visibility="{Binding Converter={StaticResource VisibilityConverterDS}, Path=IsShow6}" Height="64" Width="64"/>
 <CheckBox Content="Show" Width="64" IsChecked="{Binding Mode=TwoWay, Path=IsShow6}" Height="17"/>
 </StackPanel>
 <StackPanel Height="Auto" Width="Auto">
 <Image Source="assets/07_Calendar.png" Stretch="Fill" Visibility="{Binding Converter={StaticResource VisibilityConverterDS}, Path=IsShow7}" Height="64" Width="64"/>
 <CheckBox Width="64" Content="Show" Height="20" IsChecked="{Binding Mode=TwoWay, Path=IsShow7}"/>
 </StackPanel>
 <StackPanel Height="Auto" Width="Auto">
 <Image Source="assets/08_Links.png" Stretch="Fill" Visibility="{Binding Converter={StaticResource VisibilityConverterDS}, Path=IsShow8}" Height="64" Width="64"/>
 <CheckBox Width="64" Content="Show" Height="20" IsChecked="{Binding Mode=TwoWay, Path=IsShow8}"/>
 </StackPanel>
 <StackPanel Height="Auto" Width="Auto">
 <Image Source="assets/09_Print.png" Stretch="Fill" Visibility="{Binding Converter={StaticResource VisibilityConverterDS}, Path=IsShow9}" Height="64" Width="64"/>
 <CheckBox Width="64" Content="Show" Height="20" IsChecked="{Binding Mode=TwoWay, Path=IsShow9}"/>
 </StackPanel>
 <StackPanel Height="Auto" Width="Auto">
 <Image Source="assets/10_SocNet.png" Stretch="Fill" Visibility="{Binding Converter={StaticResource VisibilityConverterDS}, Path=IsShow10}" Height="64" Width="64"/>
 <CheckBox Width="64" Content="Show" Height="20" IsChecked="{Binding Mode=TwoWay, Path=IsShow10}"/>
 </StackPanel>
 </StackPanel>
 </StackPanel>
 </Grid>
 </UserControl>