最近在学习WPF,遇到一本入门好书,推荐给大家《Windows Presentation Foundation 4.5 Cookbook》

做项目首先要从MVVM开始,现在把他的Simple MVVM例子介绍给大家,感觉简单但很实用

1. App.xaml.cs中重载OnStartup

protected override void OnStartup(StartupEventArgs e) {
base.OnStartup(e);
var mainWindow = new MainWindow();
mainWindow.DataContext = new ImageData();
mainWindow.Show();
}

2. ViewModel的实现

 public  class ImageData:INotifyPropertyChanged
{
public ImageData()
{
_openImageFileCommand = new OpenImageFileCommand(this);
_zoomCommand = new ZoomCommand(this); }
public ICommand OpenImageFileCommand { get { return _openImageFileCommand; } }
public ICommand ZoomCommand { get { return _zoomCommand; } }
public string ImagePath
{
get { return _imagePath; }
set
{
_imagePath = value;
this.RaisePropertyChanged("ImagePath");
}
}
public double Zoom
{
get { return _zoom; }
set
{
_zoom = value;
this.RaisePropertyChanged("Zoom");
}
}
private void RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler == null)
return;
handler(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
private double _zoom = 1.0;
private string _imagePath;
private OpenImageFileCommand _openImageFileCommand;
private ZoomCommand _zoomCommand;
}

3. 下面看看View是怎么做的,除了Xaml加绑定,不再需要其它多余的代码了。

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CH07.RoutedCommands"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
x:Class="CH07.RoutedCommands.MainWindow"
d:DataContext="{d:DesignInstance Type=local:ImageData, IsDesignTimeCreatable=True}"
Title="Image Viewer" Height="" Width="">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<ToolBar FontSize="">
<Button Content="Open..." Command="{Binding OpenImageFileCommand}" Margin=""/>
<Button Content="Zoom In" Command="{Binding ZoomCommand}" CommandParameter="ZoomIn" Margin=""/>
<Button Content="Zoom Out" Command="{Binding ZoomCommand}" CommandParameter="ZoomOut" Margin=""/>
<Button Content="Normal" Command="{Binding ZoomCommand}" CommandParameter="ZoomNormal" Margin=""/>
</ToolBar>
<ScrollViewer Grid.Row="" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Image Source="{Binding ImagePath}" Stretch="None">
<Image.LayoutTransform>
<ScaleTransform ScaleX="{Binding Zoom}" ScaleY="{Binding Zoom}" />
</Image.LayoutTransform>
</Image>
</ScrollViewer>
</Grid>
</Window>

4. 还有ViewModel里的Command
ZoomCommand里的代码

   public class ZoomCommand:ICommand
{
public ZoomCommand(ImageData data)
{
_data = data;
}
public bool CanExecute(object parameter)
{
if (_data.ImagePath == null)
return false;
return _data.ImagePath.IsPicture();
} public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
private ImageData _data; public void Execute(object parameter)
{
var zoomType = (ZoomType)Enum.Parse(typeof(ZoomType), (string)parameter, true);
switch (zoomType)
{
case ZoomType.ZoomIn:
_data.Zoom *= 1.2;
break;
case ZoomType.ZoomOut:
_data.Zoom /= 1.2;
break;
case ZoomType.ZoomNormal:
_data.Zoom = 1.0;
break;
default:
break;
}
}
}
enum ZoomType
{
ZoomIn,
ZoomOut,
ZoomNormal
}

下面是OpenImageFileCommand

  public class OpenImageFileCommand:ICommand
{
public OpenImageFileCommand(ImageData data)
{
_data = data;
}
public bool CanExecute(object parameter)
{
return true;
} public event EventHandler CanExecuteChanged; private ImageData _data; public void Execute(object parameter)
{
var dlg = new OpenFileDialog
{
Filter = "图形 文件|*.jpg;*.png;*.bmp;*.gif"
}; if (dlg.ShowDialog()==true)
{
_data.ImagePath = dlg.FileName;
}
}
}

5. 其它
判断打开的文件类型

       public static bool IsPicture(this string filePath)
{
try
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(fs);
string fileClass;
byte buffer;
byte[] b = new byte[];
buffer = reader.ReadByte();
b[] = buffer;
fileClass = buffer.ToString();
buffer = reader.ReadByte();
b[] = buffer;
fileClass += buffer.ToString(); reader.Close();
fs.Close();
////255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
return ((fileClass == "" || fileClass == "" || fileClass == "" || fileClass == "") ? true : false); }
catch
{
return false;
}
}

最新文章

  1. VBA之文件筛选
  2. RHEL6.5 换源
  3. 关于malloc的一些想法
  4. IntelliJ IDEA 编译maven项目以及运行测试前编译项目
  5. Python之路【第十六篇】Django基础
  6. Awesome (and Free) Data Science Books[转]
  7. HibernateDaoSupport和HibernateTemplate
  8. javascript for in 循环时,会取到Array.prototype
  9. PERCONA-TOOLKIT 工具的安装与使用2
  10. activity theme parent 属性浅析
  11. JXL组件生成报告错误(两)
  12. php设计模式之抽象工厂模式
  13. OC中@class的使用
  14. Solr6.5.0配置solrcore图文详解
  15. Windows系统下文件的概念及c语言对其的基本操作(乙)
  16. Codeception 实战
  17. MongoDB集群管理常用命令
  18. 大受喜欢安卓触控一体机连接云端数据化管理提供例程DEMO
  19. JavaScript Concurrency model and Event Loop 并发模型和事件循环机制
  20. es6(三)

热门文章

  1. auth 认证
  2. NoSQL数据库-MongoDB和Redis
  3. SpringMVC:走通一个SpringMVC
  4. at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:)
  5. python(24)- 面向对象进阶
  6. mysql报错锦集
  7. svn 命令个
  8. Swift中的switch 和 do while
  9. JavaScript读书笔记(4)-变量、作用域和内存问题
  10. teradata培训文档 相关索引