简练的视图模型 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>
- Android之SurfaceView学习一
- 10.5 nload 命令
- 10.4 sar 命令
- javascript 技术填坑之URL
- 用css绕过同源策略跨域窃取数据
- byte与string之间的相互转换
- Linux基础(day 34)
- c++ 文件分块
- 深入学习Apache Spark和TensorFlow
- 10.16/10.17/10.18 iptables nat表应用
- C++实现int与string之间的相互转换
- 10.15 iptables filter表案例
- 利用crypto++库,实现计算string的md5值
- daemon函数结合脚本实现start, stop, restart
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法