Exemplo n.º 1
0
        /// <summary>
        /// 项目双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImgItemLeftClick(object sender, MouseButtonEventArgs e)
        {
            var subListView = XamlUtil.GetParentListView(e);
            //根据触发按钮获取点击的行
            var selected    = (DeviceSyncItem)((ListViewItem)subListView.ContainerFromElement(sender as StackPanel)).Content;
            var curTimeMill = TimeUtil.CurrentMillis();
            var lastClick   = selected.LastLeftMouseClickTime;

            selected.LastLeftMouseClickTime = curTimeMill;
            //双击才执行操作
            if (curTimeMill - lastClick < 200)
            {
                if (selected.SourceView == SyncDeviceType.PC)
                {
                    //如果文件存在于PC, 则打开文件
                    System.Diagnostics.Process.Start(Path.Combine(selected.FolderView, selected.NameView));
                }
                else
                {
                    //不允许重复调用
                    if (selected.IsCopying)
                    {
                        return;
                    }

                    //读取系统的临时文件夹
                    var winTempFolder = Path.GetTempPath();
                    if (null == winTempFolder)
                    {
                        OnTaskBarEvent?.Invoke(new MainWindowStatusNotify
                        {
                            message    = "无法获取临时文件夹",
                            alertLevel = AlertLevel.ERROR,
                        });
                        return;
                    }
                    var targetFullPath = Path.Combine(winTempFolder, selected.NameView);
                    //如果已经存在于临时文件夹, 则直接打开
                    if (System.IO.File.Exists(targetFullPath))
                    {
                        System.Diagnostics.Process.Start(targetFullPath);
                        return;
                    }

                    //发送进度消息
                    selected.IsCopying = true;
                    OnTaskBarEvent?.Invoke(new MainWindowStatusNotify
                    {
                        message     = "正在复制文件",
                        alertLevel  = AlertLevel.RUN,
                        nowProgress = 99
                    });
                    //先复制目标文件到临时文件夹, 再打开
                    using (var device = (MediaDevice)DeviceListCombobox.SelectedItem)
                    {
                        device.Connect();
                        var           filePath = Path.Combine(((DeviceDriverViewModel)DriverListCombobox.SelectedItem).ValueView, selected.FolderView, selected.NameView);
                        MediaFileInfo fileInfo = device.GetFileInfo(filePath);
                        fileInfo.CopyTo(targetFullPath);
                        device.Disconnect();
                    }
                    selected.IsCopying = false;
                    OnTaskBarEvent?.Invoke(new MainWindowStatusNotify
                    {
                        message = string.Format("已复制到临时文件夹 {0}", targetFullPath)
                    });
                    System.Diagnostics.Process.Start(targetFullPath);
                }
            }
            else
            {
                //显示文件名
                OnTaskBarEvent?.Invoke(new MainWindowStatusNotify
                {
                    message = string.Format("{0}", Path.Combine(selected.FolderView, selected.NameView))
                });
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 执行同步文件的后台任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoSyncFileTask(object sender, DoWorkEventArgs e)
        {
            var winProgress = new MainWindowStatusNotify()
            {
                alertLevel       = AlertLevel.RUN,
                animateProgress  = true,
                progressDuration = 100,
                nowProgress      = 0
            };
            var isTaskStop = false;
            //当前执行的队列编号
            var curTaskNo = 1;
            //当前队列最大元素数量
            var maxedQueueCount = mSyncTaskQueue.Count;

            while (mSyncTaskQueue.Count > 0)
            {
                var arg    = new DeviceSyncTaskArgs();
                var isPick = mSyncTaskQueue.TryDequeue(out arg);
                if (!isPick)
                {
                    continue;
                }
                //每个队列的分片进度
                double eachBaseProgress = 0;
                //筛选WPD设备
                using (var device = arg.Device)
                {
                    device.Connect();

                    //检查目标文件夹是否存在, 不存在则创建
                    if (!Directory.Exists(arg.Item.PcFolderNameView))
                    {
                        FileUtil.CreateFolder(arg.Item.PcFolderNameView);
                    }
                    var targetMobileFolder = Path.Combine(arg.DevicePath, arg.Item.MobileFolderNameView);
                    if (!device.DirectoryExists(targetMobileFolder))
                    {
                        device.CreateDirectory(targetMobileFolder);
                    }

                    //将两端的文件合并到一个集合
                    var allItems = arg.Item.PcItemList.ToList();
                    allItems.AddRange(arg.Item.MobileItemList.ToList());
                    var fileCounter = 1;
                    allItems.ForEach((syncFile) => {
                        //检查任务是否取消
                        if (mSyncFileBgWorker.CancellationPending)
                        {
                            isTaskStop = true;
                            return;
                        }
                        //如果队列数量增大超过初始量, 记录一个最大数量
                        if (mSyncTaskQueue.Count + 1 > maxedQueueCount)
                        {
                            maxedQueueCount = mSyncTaskQueue.Count + 1;
                        }
                        //当前队列数量=此队列拥有过的最大数量
                        var curQueueCount = maxedQueueCount;
                        //计算每个队列的分片进度
                        eachBaseProgress = 100 / curQueueCount;
                        //每个队列的基础进度, 从0开始, 以分片进度递增
                        double baseProgress = 100 - (mSyncTaskQueue.Count + 1) * eachBaseProgress;
                        //计算每个文件的分片进度
                        double eachFileProgress = eachBaseProgress / allItems.Count;
                        //文件的执行进度, 从0开始递增
                        double fileProgress = eachFileProgress * fileCounter - eachFileProgress;
                        //总进度 = 基础进度 + 文件进度
                        WindowUtil.CalcProgress(winProgress,
                                                string.Format("主任务 [{0}/{1}], 子任务 [{2}/{3}], 正在复制 [ {4} ]", curTaskNo, curQueueCount, fileCounter, allItems.Count, syncFile.NameView),
                                                baseProgress + fileProgress);
                        arg.Progress     = winProgress;
                        arg.ProgressType = SyncTaskProgressType.SUB_ITEM_FINISH;
                        mSyncFileBgWorker.ReportProgress(0, arg);
                        //执行同步
                        if (syncFile.SourceView == SyncDeviceType.PC)
                        {
                            //电脑端, 需要复制到移动端
                            var targetFolder = Path.Combine(arg.DevicePath, arg.Item.MobileFolderNameView);
                            using (FileStream stream = System.IO.File.OpenRead(Path.Combine(arg.Item.PcFolderNameView, syncFile.NameView)))
                            {
                                device.UploadFile(stream, Path.Combine(targetFolder, syncFile.NameView));
                            }
                        }
                        else
                        {
                            MediaFileInfo fileInfo = device.GetFileInfo(Path.Combine(arg.DevicePath, arg.Item.MobileFolderNameView, syncFile.NameView));
                            fileInfo.CopyTo(Path.Combine(arg.Item.PcFolderNameView, syncFile.NameView));
                        }
                        fileProgress = eachFileProgress * fileCounter;
                        WindowUtil.CalcProgress(winProgress,
                                                string.Format("主任务 [{0}/{1}], 子任务 [{2}/{3}], 正在复制 [ {4} ]", curTaskNo, curQueueCount, fileCounter, allItems.Count, syncFile.NameView),
                                                baseProgress + fileProgress);
                        arg.Source         = syncFile.SourceView;
                        arg.DeviceSyncItem = syncFile;
                        mSyncFileBgWorker.ReportProgress(0, arg);
                        fileCounter++;
                    });
                    device.Disconnect();
                }
                arg.IsOk         = !isTaskStop;
                arg.ProgressType = SyncTaskProgressType.QUEUE_FINISH;
                if (!isTaskStop)
                {
                    //一个队列执行完之后, 发送当前队列进度
                    WindowUtil.CalcProgress(arg.Progress, string.Format("主任务 [{0}/{1}]同步完成", curTaskNo, maxedQueueCount), curTaskNo * eachBaseProgress);
                    mSyncFileBgWorker.ReportProgress(0, arg);
                    curTaskNo++;
                }
                else
                {
                    mSyncFileBgWorker.ReportProgress(0, arg);
                }
            }
            e.Result = !isTaskStop;
        }