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


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
}
}


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
}
}


<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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述