private void UpdateTasekViewUI() { App.Current.Dispatcher.BeginInvoke(new Action(() => { lock (TaskListLocker) { var _up = new TaskViewModel(); TaskViewList.Add(_up); TaskViewList.Remove(_up); } })); }
public WindowView() { InitCef(); DownloadHandler.OnUpdated += WindowView_OnUpdated; WebControl = new CustomCommand(CommandJudger, CommandExcter); TaskControl = new CustomCommand( o => { //return true; var _cmds = (object[])o; var cmd = (string)_cmds[0]; var target = (TaskViewModel)_cmds[1]; switch (cmd) { case "_Pause": return(!target.IsPaused && !target.IsPauseing); case "_Stop": return(!target.IsCompleted && !target.IsErrored); case "_Resume": return(target.IsPaused); case "_CopyUrl": return(!string.IsNullOrWhiteSpace(target.Url)); case "_Drop": return(true); case "_Remove": return(target.IsCompleted || target.IsStopped); case "_Retry": return(target.IsStopped || target.IsErrored); case "_Open": return(!string.IsNullOrWhiteSpace(target.Path)); default: return(true); } } , p => { var _cmds = (object[])p; var cmd = (string)_cmds[0]; var target = (TaskViewModel)_cmds[1]; switch (cmd) { case "_Pause": target.IsPauseing = true; break; case "_Stop": target.IsStopping = true; target.IsPaused = false; break; case "_Resume": target.IsResuming = true; target.Chrome.Info = target.Chrome.PopTask(); target.Chrome.Address = target.Chrome.Info.Action; target.IsResuming = false; target.IsPaused = false; break; case "_CopyUrl": if (!string.IsNullOrWhiteSpace(target.Url)) { Clipboard.SetText(target.Url); } break; case "_Drop": if (MessageBox.Show(string.Format("注意,此操作无法恢复!是否确认移除任务集[{0}]产生的所有数据?", target.Title), "移除警告", MessageBoxButton.OKCancel) == MessageBoxResult.OK) { target.IsStopping = true; App.Current.Dispatcher.BeginInvoke(new Action(() => { //等待任务终止 while (target.IsStopped == false) { Thread.Sleep(300); } TaskViewList.Remove(target); Directory.Delete(target.Path, true); })); } break; case "_Remove": TaskViewList.Remove(target); break; case "_Retry": break; case "_Open": System.Diagnostics.Process.Start(Path.GetFullPath(target.Path)); break; default: break; } }); SearchControl = new CustomCommand( o => { if (o is string && !string.IsNullOrWhiteSpace((string)o)) { return(true); } return(false); }, p => { var _key = p as string; _key = "https://www.baidu.com/s?wd=" + _key; OpenUrl(_key); }); //OpenurlControl = new CustomCommand( // o =>{ // if (o is string && !string.IsNullOrWhiteSpace((string)o)) return true; // return false; // }, // p => // { // OpenUrl(p as string); // }); DownloadControl = new CustomCommand( o => { var cmds = o as object[]; var target = (DownloadItemViewModel)cmds[1]; if (target == null) { return(false); } switch (cmds[0] as string) { case "_Cancel": return(!target.IsComplete && !target.IsCancelled && !target.IsValid); case "_Pause": return(target.IsValid && !target.IsPaused && !target.IsComplete && !target.IsCancelled); case "_Resume": return(target.IsPaused); case "_Open": return(!string.IsNullOrWhiteSpace(target.FullPath)); case "_Excute": return(!string.IsNullOrWhiteSpace(target.FullPath)); case "_CopyUrl": return(!string.IsNullOrWhiteSpace(target.Url)); case "_Drop": return(true); case "_Retry": return(!target.IsValid || target.IsCancelled); case "_Remove": return(target.IsComplete || target.IsCancelled); default: return(false); } }, p => { var cmds = p as object[]; var target = (DownloadItemViewModel)cmds[1]; switch (cmds[0] as string) { case "_Cancel": //target.NeedCancel = true; target.customDownloadHandler.Cancel(target.ID); break; case "_Pause": //target.NeedPause = true; target.customDownloadHandler.Pause(target.ID); break; case "_Resume": //target.NeedResume = true; target.customDownloadHandler.Resume(target.ID); break; case "_Open": System.Diagnostics.Process.Start(Path.GetDirectoryName(target.FullPath)); break; case "_Excute": System.Diagnostics.Process.Start(target.FullPath); break; case "_CopyUrl": Clipboard.SetText(target.Url); break; case "_Drop": if (MessageBox.Show(string.Format("是否确认删除文件[{0}]并移除记录?此操作无法恢复!", target.FullPath), "移除警告", MessageBoxButton.OKCancel) == MessageBoxResult.OK) { DownloadList.Remove(target); if (!target.IsComplete) { target.NeedCancel = true; } else { File.Delete(target.FullPath); } } break; case "_Retry": break; case "_Remove": DownloadList.Remove(target); break; default: break; } }); MenuControl = new CustomCommand( o => { if (o is string) { switch (o as string) { case "_OpenDownload": return(Directory.Exists("./Download/")); case "_OpenLog": return(Directory.Exists("./Log/")); case "_OpenAbout": return(true); default: break; } } return(false); }, p => { if (p is string) { switch (p as string) { case "_OpenRules": var path = Path.GetFullPath("./Rules.txt"); if (!File.Exists(path)) { File.WriteAllText(path, "@请添加解析规则,重启应用程序后生效;"); } System.Diagnostics.Process.Start(path); break; case "_OpenDownload": System.Diagnostics.Process.Start(Path.GetFullPath("./Download/")); break; case "_OpenLog": System.Diagnostics.Process.Start(Path.GetFullPath("./Log/")); break; case "_OpenAbout": MessageBox.Show("什么都没有~"); break; default: break; } } }); CmdHelper.Load("./Rules.txt"); BK = new BackgroundWorker(); BH = new BrowserHelper <CustomChrominWebBrowser>(); BH.SetPoolSize(3); LifeSpanHandler.OpenTargUrl += OpenTargetUrl; BK.WorkerReportsProgress = false; BK.WorkerSupportsCancellation = true; BK.DoWork += BK_DoWork; BK.RunWorkerCompleted += BK_RunWorkerCompleted; //DownloadList.Add(new DownloadItemViewModel() { FullPath = @"E:\Windows\Pictures\笑哭.jpg", FileName = "笑哭.jpg", TotalBytes = 4096, ReceivedBytes = 1024, CurrentSpeed = 1099, EndTime = DateTime.Now, IsComplete = true }); //DownloadList.Add(new DownloadItemViewModel() { FullPath = @"E:\Windows\Pictures\笑哭.jpg", FileName = "笑哭.jpg", TotalBytes = 4096, ReceivedBytes = 1024, CurrentSpeed = 1099, EndTime = DateTime.Now, IsPaused = true }); //DownloadList.Add(new DownloadItemViewModel() { FullPath = @"E:\Windows\Pictures\笑哭.jpg", FileName = "笑哭.jpg", TotalBytes = 4096, ReceivedBytes = 1024, CurrentSpeed = 1099, EndTime = DateTime.Now, IsValid = false }); //DownloadList.Add(new DownloadItemViewModel() { FullPath = @"E:\Windows\Pictures\笑哭.jpg", FileName = "笑哭.jpg", TotalBytes = 4096, ReceivedBytes = 1024, CurrentSpeed = 1099, EndTime = DateTime.Now, IsCancelled = true }); //DownloadList.Add(new DownloadItemViewModel() { FullPath = @"E:\Windows\Pictures\笑哭.jpg", FileName = "笑哭.jpg", TotalBytes = 4096, ReceivedBytes = 1024, CurrentSpeed = 1099, EndTime = DateTime.Now, IsInProgress = true }); //TaskViewList.Add(new TaskViewModel() { ID = 9990099, Title = "艾玛A网站", TotalCount = 67, ErrorCount = 10, SuccessCount = 48, IsCompleted = true }); //TaskViewList.Add(new TaskViewModel() { ID = 9994799, Title = "艾玛B网站", TotalCount = 67, ErrorCount = 10, SuccessCount = 48, IsErrored = true }); //TaskViewList.Add(new TaskViewModel() { ID = 9994599, Title = "艾玛C网站", TotalCount = 67, ErrorCount = 10, SuccessCount = 48, IsPaused = true }); //TaskViewList.Add(new TaskViewModel() { ID = 9994399, Title = "艾玛D网站", TotalCount = 67, ErrorCount = 10, SuccessCount = 48, IsPaused = false }); //TaskViewList.Add(new TaskViewModel() { ID = 9994199, Title = "艾玛E网站", TotalCount = 67, ErrorCount = 10, SuccessCount = 48, IsStopped = true }); }