前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >简练的视图模型 ViewModel

简练的视图模型 ViewModel

作者头像
用户1172164
发布2018-01-16 15:06:35
8720
发布2018-01-16 15:06:35
举报
文章被收录于专栏:我和未来有约会

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

Get Microsoft Silverlight
Get Microsoft Silverlight

这里是我的代码:

代码语言:js
复制
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
     }
 }
 
代码语言:js
复制
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
     }
 }
 

代码语言:js
复制
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>
 
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2008-12-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档