using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Cinch;
using System.Windows.Forms;
using System.Windows.Threading;
namespace PIC
{
public class VM:ViewModelBase
{
/// <summary>
/// 路径
/// </summary>
public string FolderPath { get; private set; }
//public IList<string> imageList{get;set;}
public string ImagePath { get; set; }
private int index = 0;
//private string path = System.Environment.CurrentDirectory;
DispatcherTimer timer = new DispatcherTimer();
/// <summary>
/// 图片文件名
/// </summary>
public IList<string> ImageFileList { get; private set; }
public SimpleCommand ChangeFolderCommand { get; private set; }
public SimpleCommand PreviousImageCommand { get; private set; }
public SimpleCommand NextImageCommand { get; private set; }
public SimpleCommand ClockWiseCommand { get; private set; }
public SimpleCommand CounterClockWiseCommand { get; private set; }
public SimpleCommand StartSlideShowCommand { get; private set; }
public SimpleCommand StopSlideShowCommand { get; private set; }
public SimpleCommand DeleteImageCommand { get; private set; }
/// <summary>
///
/// </summary>
/// <param name="folderPath"></param>
public VM(string folderPath)
{
this.ImageFileList = new List<string>();
ChangeFolderPath(folderPath);
this.ChangeFolderCommand = new SimpleCommand
{
ExecuteDelegate=x=>ChangeFolder()
};
this.PreviousImageCommand = new SimpleCommand
{
ExecuteDelegate = x => PreviousImage()
};
this.NextImageCommand = new SimpleCommand
{
ExecuteDelegate = x => NextImage()
};
this.StopSlideShowCommand = new SimpleCommand
{
ExecuteDelegate =x=>StopSlideShow()
};
this.DeleteImageCommand = new SimpleCommand
{
ExecuteDelegate=x=>DeleteImage()
};
timer.Interval = TimeSpan.FromMilliseconds(1000);
timer.Tick += new EventHandler(timer_Tick);
}
void timer_Tick(object sender, EventArgs e)
{
if (index >= ImageFileList.Count)
{
index = 0;
}
ImagePath = ImageFileList[index];
index++;
}
void StopSlideShow()
{
timer.Stop();
}
void ChangeFolder()
{
FolderBrowserDialog dialog=new FolderBrowserDialog();
if(dialog.ShowDialog()==DialogResult.OK)
{
ChangeFolderPath(ImagePath);
ImageFileList.Clear();
//
}
}
void PreviousImage()
{
index--;
if (index < 0)
{
MessageBox.Show("已是第一张");
return;
}
ImagePath=ImageFileList[index];
}
void NextImage()
{
index--;
if (index >ImageFileList.Count-1)
{
MessageBox.Show("已是最后一张");
return;
}
ImagePath = ImageFileList[index];
}
void StartSlideShow()
{
timer.Start();
}
void DeleteImage()
{
if (ImagePath != null)
// if (_deletingImg != null)
{
if (MessageBox.Show("是否删除", "提示", MessageBoxButtons.YesNo) ==
System.Windows.Forms.DialogResult.Yes)
{
try
{
//this.pictureBox.Image.Dispose();
//this.pictureBox.Image = null;
DeleteImage(ImagePath);
}
catch (Exception ex)
{
MessageBox.Show("删除失败", ex.ToString(), MessageBoxButtons.OK);
//throw;
}
finally
{
//this.LoadImage();
}
}
}
else
{
MessageBox.Show("选择图片");
}
}
/// <summary>
/// 验证路径合法性
/// </summary>
/// <param name="folderPath"></param>
void ValidateFilePath(string folderPath)
{
if (Directory.Exists(folderPath) == false)
{
throw new Exception("错误");
}
return;
}
/// <summary>
/// 更改文件路径
/// </summary>
/// <param name="folderPath"></param>
public void ChangeFolderPath(string folderPath)
{
ValidateFilePath(folderPath);
this.FolderPath = folderPath;
ReadImages();
}
/// <summary>
/// 读取文件资源
/// </summary>
void ReadImages()
{
//清空原有的文件路径
//遍历新文件夹路径下的文件
//填充图片文件名
// ImageFiles
string[] imagePath;
ImageFileList.Clear();
try
{
imagePath = Directory.GetFiles(FolderPath, "*.jpg");
for (int i = 0; i < imagePath.Length; i++)
{
ImageFileList.Add(imagePath[i]);
}
}
catch (Exception e)
{
throw new Exception("错误", e);
}
}
void DeleteImage(string imageFileName)
{
if (File.Exists(imageFileName) == true)
{
try
{
File.Delete(imageFileName);
ImageFileList.Remove(imageFileName);
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}
}
}
}
写了这个个ViewModel,然后我用Nunit测试,我就在测试类里写了句
VM v = new VM(System.Environment.CurrentDirectory);
用Nunit来Debug一到vm的构造就报“未能加载文件或程序集“Microsoft.Practices.Unity.Configuration, Version=2.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。系统找不到指定的文件。”怎么搞
9 个解决方案
#1
DLL?..............
#2
你是不是引用了一个framework2.0的dll?
#3
1.是否缺少;
2.版本是否正确;
2.版本是否正确;
#4
如果是的话在config文件里面加
<startup useLegacyV2RuntimeActivationPolicy="true">
</startup>
试试看。
#5
自从我建这个工程开始就没这个app.config文件
#6
没有可以自己建一个。前提是确定dll引用版本原因
#7
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
</startup>
</configuration>
我建了一个,改成这样,还是刚才报老错误
#8
.dll没导入。
#9
我知道了,是我引的Cinch库出问题了,我去掉cinch库程序就走下去了
#1
DLL?..............
#2
你是不是引用了一个framework2.0的dll?
#3
1.是否缺少;
2.版本是否正确;
2.版本是否正确;
#4
如果是的话在config文件里面加
<startup useLegacyV2RuntimeActivationPolicy="true">
</startup>
试试看。
#5
自从我建这个工程开始就没这个app.config文件
#6
没有可以自己建一个。前提是确定dll引用版本原因
#7
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
</startup>
</configuration>
我建了一个,改成这样,还是刚才报老错误
#8
.dll没导入。
#9
我知道了,是我引的Cinch库出问题了,我去掉cinch库程序就走下去了