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 DeleteItemCommand { get; set; } public DelegateCommand ReLoadDataCommand { get; set; } public DelegateCommand<FunctionEventArgs<int>> PageUpdatedCmd { get; set; } #endregion private ImageResultFile _selectedFile; public PictureFileViewModel() { GoBackCommand = new DelegateCommand(delegate { RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel)); }); ItemSelectedCommand = new DelegateCommand<DataGrid>(delegate(DataGrid dataGrid) { _selectedFile = (ImageResultFile)dataGrid.SelectedItem; }); DeleteItemCommand = new DelegateCommand(() => { if (_selectedFile == null) { Growl.Error("删除失败"); return; } try { File.Delete(_selectedFile.FullPath); //刷新数据 _totalFiles = GetTotalPictureFiles(); MaxPage = _totalFiles.Count / 8 + 1; PictureFiles = _totalFiles.Take(8).ToList(); } catch (IOException) { Growl.Error("删除失败"); } }); ReLoadDataCommand = new DelegateCommand(() => { Growl.Success("刷新成功"); }); PageUpdatedCmd = new DelegateCommand<FunctionEventArgs<int>>(args => { PictureFiles = _totalFiles.Skip((args.Info - 1) * 8).Take(8).ToList(); }); } public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } public void OnDialogOpened(IDialogParameters parameters) { _totalFiles = GetTotalPictureFiles(); MaxPage = _totalFiles.Count / 8 + 1; PictureFiles = _totalFiles.Take(8).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(); } } }