WPF MVVM双击打开文件

本项目是WPF 使用Mvvm设计模式,进行开发的桌面软件工程
需求中需要还软件中双击打开软件内的的文件。
v 为一个页面中item的子项。双击该子项打开子项中的文件

  1. 使用触发器进行操作,通过触发双击命令打开,但由于打开的相关命令需要传多参数。触发器传值存在问题(使用触发器进行传值,可指定相关对象)
    如果不需要多参数传值,可使用该方法,通过为当前的控件添加触发,添加双击触发执行对应的命令。
  2. vm中使用统计功能,对点击次数进行判断,如当前软件的文件被点击的次数大于或等于两次执行打开打操作
    点击的是不是当前的对象,点击了多少次
int clickNub = 0;//定义一个计数,统计点击次数
double timespan = 0;//定义时间戳
private void OpenOffice(object[] objArr)
        {
            clickNub++;//每点击一次加一
            var fileInfo = objArr[0] as IssuesData;//提取内容
            if (clickNub%2==0)
            {
               
                if (Toolkit.Utilities.Common.GetTimeStamp() - timespan > 1200||fileid!= fileInfo.id.ToString())//当前时间内点击的对象
                {
                    clickNub = 1;//置1
                  
                } 
            }
            fileid = fileInfo.id.ToString();
            timespan = Toolkit.Utilities.Common.GetTimeStamp();

            if ((objArr[1]?.ToString().ToLower() == "false"))
            {
                clickNub = 2;
            }
            if (clickNub % 2 == 0)
            {
                clickNub = 0;
              
                Progress = 0;
                IsLoading = true;
               
                if (fileInfo.fileType == 0)
                {
                    //文件夹
                    GetMeetingDatabyfloider(fileInfo.id);
                    _SendBackPath(fileInfo.id, fileInfo.fileName, true);
                }

                if (fileInfo.fileSuffix.ToUpper().Contains("PDF"))
                {
                    OfficeMinimizeWindow info = new OfficeMinimizeWindow();
                    info.id = fileInfo.id;
                    if (_IsSaveintheOfficeinimizeList == null)
                    {
                        PdfName = fileInfo.fileName;
                        Pdfid = fileInfo.id;
                        DownPdfImage(objArr);
                        //var result=
                    }
                    else
                    {
                        if (!_IsSaveintheOfficeinimizeList(info))//判断是否已经存在用于最小化防止多次打开
                        {
                            IsLoading = false;
                            //PdfName = fileInfo.fileName;
                            //Pdfid = fileInfo.id;
                            //DownPdfImage(objArr);
                        }
                        else
                        {
                            PdfName = fileInfo.fileName;
                            Pdfid = fileInfo.id;
                            DownPdfImage(objArr);
                        }
                    }
                }
                else
                {
                    var update = new Action<double>(p =>
                    {

                        DispatcherHelper.CheckBeginInvokeOnUI(() =>
                        {
                            Progress = p;
                        });
                    });
                    Task.Factory.StartNew(() =>
                    {
                        try
                        {
                            if (fileInfo.fileType != 0)
                            {
                                IsLoading = true;
                                string savePathFlag = Global.MeetingFilesPath + Global.MeetingId + "\\" + fileInfo.meetingIssuesId;
                                string path = HttpRequester.DownloadData(fileInfo.filePath, savePathFlag, fileInfo.fileName, update);
                                if (!string.IsNullOrWhiteSpace(path))
                                {
                                    IsLoading = false;
                                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                                    {
                                        try
                                        {
                                            remark = new ReMarkWindow();
                                            remark.Hide();
                                        }
                                        catch (Exception) { remark.Close(); }

                                        if (Global.document.Contains(fileInfo.fileSuffix.ToLower()))
                                        {
                                            if (bool.Parse(objArr[1].ToString()))
                                            {
                                                string destPath = $"{System.IO.Path.GetDirectoryName(path)}\\只读{System.IO.Path.GetFileName(path)}";
                                                if (!System.IO.File.Exists(destPath))
                                                {
                                                    System.IO.File.Copy(path, destPath);
                                                    System.IO.File.SetAttributes(destPath, System.IO.FileAttributes.ReadOnly);
                                                }

                                                OfficeMinimizeWindow officeMinimize = new OfficeMinimizeWindow();
                                                officeMinimize.id = fileInfo.id;

                                                if (_IsSaveintheOfficeinimizeList == null)
                                                {
                                                    if (remark != null && remark.OpenReadOnly(destPath))
                                                    {
                                                        remark.Show();
                                                        //将office文档的名称传入到RemarkWindow
                                                        _SendDataName(fileInfo.fileName, fileInfo.id, openType);
                                                    }
                                                    else
                                                    {
                                                        System.Diagnostics.Process.Start(destPath);
                                                    }
                                                }
                                                else
                                                {
                                                    if (!_IsSaveintheOfficeinimizeList(officeMinimize))
                                                    {
                                                        IsLoading = false;
                                                        remark.Close();
                                                    }
                                                    else
                                                    {
                                                        //remark.Show();
                                                        ////将office文档的名称传入到RemarkWindow
                                                        //_SendDataName(fileInfo.fileName, fileInfo.id);
                                                        if (remark != null && remark.OpenReadOnly(destPath))
                                                        {
                                                            remark.Show();
                                                            //将office文档的名称传入到RemarkWindow
                                                            _SendDataName(fileInfo.fileName, fileInfo.id, openType);
                                                        }
                                                        else
                                                        {
                                                            System.Diagnostics.Process.Start(destPath);
                                                        }
                                                    }

                                                }
                                            }
                                            else
                                            {
                                                //string destPath = $"{Global.NoteFileFilesPath}\\{System.IO.Path.GetFileNameWithoutExtension(path)}_{Global.UserName}批注_{Toolkit.Utilities.Common.GetDateNow()}{System.IO.Path.GetExtension(path)}";
                                                string destPath = $"{Global.NoteFileFilesPath}\\{System.IO.Path.GetFileNameWithoutExtension(path)}_{Global.UserName}批注_{DateTime.Now.ToString("yyyyMMddHHmmss")}{System.IO.Path.GetExtension(path)}";
                                                if (remark != null && remark.OpenReMark(path, destPath))
                                                {
                                                    remark.Show();
                                                    //将office文档的名称传入到RemarkWindow
                                                    _SendDataName(fileInfo.fileName, fileInfo.id, openType);
                                                }
                                                else
                                                {
                                                    _saveWatcher.Start();
                                                    _saveWatcher.Open(path, destPath);
                                                    //System.Diagnostics.Process.Start(destPath);
                                                }
                                                //_saveWatcher.Open(path, destPath);
                                            }
                                        }
                                        else
                                        if (fileInfo.fileSuffix.Equals("png") || fileInfo.fileSuffix.Equals("jpg") || fileInfo.fileSuffix.Equals("JPG") || fileInfo.fileSuffix.Equals("PNG"))
                                        {
                                            WindowSingleton.Show<PictureUC>();
                                            List<string> _picpath = new List<string>();
                                            for (int i = 0; i < PictureData.Count; i++)
                                            {
                                                string PicPath = HttpRequester.DownloadData(PictureData[i].filePath, savePathFlag, PictureData[i].fileName, update);
                                                _picpath.Add(PicPath);
                                            }
                                            _Picpath(path, _picpath);
                                            _piclist(_picpath);
                                        }
                                        else if (fileInfo.fileType == 3)
                                        {
                                            WindowSingleton.Show<VideoUC>();
                                            _VedioUrlList(fileInfo.filePath, VedioData, savePathFlag);
                                            IsLoading = false;
                                        }
                                        else
                                        {
                                            System.Diagnostics.Process.Start(path);
                                        }
                                    });
                                }
                            }
                            else if (fileInfo.fileType == 0)
                            {
                                IsLoading = false;
                            }
                        }
                        catch (Exception ex)
                        {
                            DispatcherHelper.CheckBeginInvokeOnUI(() =>
                            {
                                IsLoading = false;
                                MessageBox.Show(ex);
                            });
                        }
                    });
                }
            }
        }

总结:上面的俩方法都能实现双击打开的操作,相对于第二个方法。第一个方法更为简单,但通过触发器传参,多个值的时候很麻烦。
使用第二个方法时,需考虑各种情况。

posted @ 2022-07-04 12:01  CodeBo  阅读(170)  评论(0)    收藏  举报