using PDF_Office.Model; using PDF_Office.Model.PageEdit; using Prism.Commands; using Prism.Mvvm; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace PDF_Office.ViewModels.Dialog.PageEditDialogs { public class InsertDialogViewModel : BindableBase, IDialogAware { public string Title =>""; /// /// 数据模型 /// private CustomInsertModel Model = new CustomInsertModel(); public event Action RequestClose; private string currentPageSize; /// /// 当前页的尺寸大小 括号显示的形式 /// public string CurrentPageSize { get { return currentPageSize; } set { SetProperty(ref currentPageSize, value); } } private int itemSelectedIndex = 0; /// /// 自定义页面的选中索引 /// public int ItemSelectedIndex { get { return itemSelectedIndex; } set { SetProperty(ref itemSelectedIndex, value); if(Model!=null) { Model.filepath = Pages[itemSelectedIndex].FilePath; } } } private string customWidth; /// /// 自定义页面宽度 /// public string CustomWidth { get { return customWidth; } set { SetProperty(ref customWidth, value); } } private string customHeight; /// /// 自定义页面高度 /// public string CustomHeight { get { return customHeight; } set { SetProperty(ref customHeight, value); } } private int unitsSelectedIndex = 0; /// /// 单位下拉框的选中项索引 /// public int UnitsSelectedIndex { get { return unitsSelectedIndex; } set { SetProperty(ref unitsSelectedIndex, value); } } /// /// 自定义页面的路径集合 /// public ObservableCollection Pages { get; set; } /// /// 页面单位集合 /// public List Units { get; set; } public DelegateCommand CancelCommand { get; set; } public DelegateCommand InsertCommnad { get; set; } /// /// 页面方向选择的事件 /// public DelegateCommand OrientationCheckedCommand { get; set; } public InsertDialogViewModel() { InitPageSource(); InitUnits(); CancelCommand = new DelegateCommand(cancel); InsertCommnad = new DelegateCommand(insert); OrientationCheckedCommand = new DelegateCommand(OrientationChecked); } /// /// 初始化页面大小单位集合 /// private void InitUnits() { Units = new List(); Units.Add("mm"); Units.Add("cm"); Units.Add("in"); } /// /// 初始化自定义页面集合 /// private void InitPageSource() { Pages = new ObservableCollection(); Pages.Add(new CustomPageItem() {Name="空白页",FilePath = ""}); Pages.Add(new CustomPageItem() { Name="横线",FilePath= System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\HorizontalLine.jpg")}); Pages.Add(new CustomPageItem() { Name = "五线谱", FilePath = System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\Staff.jpg") }); Pages.Add(new CustomPageItem() { Name = "格子线", FilePath = System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\GridLine.jpg") }); } /// /// 页面方向选中事件 /// /// private void OrientationChecked(string orientation) { switch (orientation) { //如果宽高不符合条件就对调宽高 case "Vertical": if(Model.height<=Model.width) { var temp = Model.height; Model.height = Model.width; Model.width = Model.height; } break; case "Horizontal": if (Model.height> Model.width) { var temp = Model.height; Model.height = Model.width; Model.width = Model.height; } break; default: break; } } private void cancel() { RequestClose.Invoke(new DialogResult(ButtonResult.Cancel)); } private void insert() { DialogParameters valuePairs = new DialogParameters(); valuePairs.Add(ParameterNames.DataModel, Model); RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs)); } #region 弹窗接口 public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } public void OnDialogOpened(IDialogParameters parameters) { if(parameters!=null) { var size = parameters.GetValue("CurrentPageSize"); CurrentPageSize = $"({size.Width.ToString("F2")}mm*{size.Height.ToString("F2")}mm)"; CustomWidth = size.Width.ToString("F2"); CustomHeight = size.Height.ToString("F2"); Model.width = (int)size.Width; Model.height = (int)size.Height; } } #endregion } }