using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Windows.Controls; using Correlator.Model; using Correlator.Util; using HandyControl.Controls; using HandyControl.Data; using Prism.Commands; using Prism.Mvvm; using Prism.Services.Dialogs; namespace Correlator.ViewModels { public class PictureFileViewModel : BindableBase, IDialogAware { #region 属性绑定 public string Title { get; private set; } = string.Empty; public event Action<IDialogResult> RequestClose; private List<ImageResultFile> _totalFiles; private List<ImageResultFile> _pictureFiles; public List<ImageResultFile> PictureFiles { get => _pictureFiles; set { _pictureFiles = value; RaisePropertyChanged(); } } private int _pageIndex = 1; public int PageIndex { get => _pageIndex; set { _pageIndex = value; RaisePropertyChanged(); } } private int _maxPage; public int MaxPage { get => _maxPage; set { _maxPage = value; RaisePropertyChanged(); } } #endregion #region DelegateCommand public DelegateCommand GoBackCommand { get; set; } public DelegateCommand<DataGrid> ItemSelectedCommand { get; set; } public DelegateCommand ShowPictureCommand { get; set; } public DelegateCommand DeletePictureCommand { get; set; } public DelegateCommand<FunctionEventArgs<int>> PageUpdatedCmd { get; set; } #endregion private ImageResultFile _selectedFile; public PictureFileViewModel(IDialogService dialogService) { GoBackCommand = new DelegateCommand(delegate { RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel)); }); ItemSelectedCommand = new DelegateCommand<DataGrid>(delegate(DataGrid dataGrid) { _selectedFile = (ImageResultFile)dataGrid.SelectedItem; }); ShowPictureCommand = new DelegateCommand(delegate { if (_selectedFile == null) { dialogService.ShowDialog( "AlertMessageDialog", new DialogParameters { { "AlertType", AlertType.Error }, { "Title", "温馨提示" }, { "Message", "图片文件路径错误,无法查看" } }, delegate { } ); return; } dialogService.ShowDialog("BigPictureView", new DialogParameters { { "ImageFullPath", _selectedFile.FullPath } }, delegate { }); }); DeletePictureCommand = new DelegateCommand(() => { if (_selectedFile == null) { Growl.Error("删除失败"); return; } try { File.Delete(_selectedFile.FullPath); //刷新数据 _totalFiles = GetTotalPictureFiles(); MaxPage = _totalFiles.Count.GetPageSize(); PictureFiles = _totalFiles.Take(RuntimeCache.PerPageItemCount).ToList(); } catch (IOException) { Growl.Error("删除失败"); } }); PageUpdatedCmd = new DelegateCommand<FunctionEventArgs<int>>(args => { PictureFiles = _totalFiles .Skip((args.Info - 1) * RuntimeCache.PerPageItemCount) .Take(RuntimeCache.PerPageItemCount) .ToList(); }); } public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } public void OnDialogOpened(IDialogParameters parameters) { _totalFiles = GetTotalPictureFiles(); MaxPage = _totalFiles.Count.GetPageSize(); PictureFiles = _totalFiles.Take(RuntimeCache.PerPageItemCount).ToList(); } private static List<ImageResultFile> GetTotalPictureFiles() { var i = 1; return new DirectoryInfo(DirectoryManager.GetPictureDir()).GetFiles("*", SearchOption.AllDirectories) .Select(file => new ImageResultFile { Order = i++, FileName = file.Name, PersonLiable = "", FullPath = DirectoryManager.GetPictureDir() + "\\" + file.Name, FileSize = file.Length.FormatFileSize(), CreationTime = file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss") }).ToList(); } } }