public void EnableHotReload(string baseLocation) { if (string.IsNullOrEmpty(baseLocation)) { throw new InvalidOperationException("Hot reload does not work in release mode"); } baseLocation = Path.GetDirectoryName(baseLocation); baseLocation = Path.GetFullPath(baseLocation + "\\..\\.."); if (this.fileSystemWatcher != null) { this.fileSystemWatcher.Path = baseLocation; return; } this.fileSystemWatcher = new FileSystemWatcher(baseLocation); this.fileSystemWatcher.IncludeSubdirectories = true; this.fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite; this.fileSystemWatcher.EnableRaisingEvents = true; bool filesChanged = false; string[] fileExtensionsToWatch = new string[] { ".js", ".css" }; this.fileSystemWatcher.Changed += delegate(object sender, FileSystemEventArgs eventArgs) { if (this.IsReady) { filesChanged = true; this.webView.BeginInvoke(new Action(() => { if (this.IsReady) { this.IsReady = false; this.cacheInvalidationTimestamp = DateTime.UtcNow.Ticks.ToString(); this.webView.Reload(true); } })); } }; this.webView.BeforeResourceLoad += delegate(CefSharpMe.ResourceHandler resourceHandler) { if (filesChanged) { Uri resourceUrl = new Uri(resourceHandler.Url); string path = Path.Combine(ResourceUrl.GetEmbeddedResourcePath(resourceUrl).Skip(1).ToArray <string>()); if (fileExtensionsToWatch.Any((string e) => path.EndsWith(e))) { path = Path.Combine(this.fileSystemWatcher.Path, path); if (new FileInfo(path).Exists) { resourceHandler.RespondWith(path); } } } }; }
/// <summary> /// 加载嵌入的资源 /// </summary> /// <param name="resourceHandler"></param> /// <param name="url"></param> internal virtual void LoadEmbeddedResource(CefSharpMe.ResourceHandler resourceHandler, Uri url) { Assembly assembly = this.ResolveResourceAssembly(url); string[] embeddedResourcePath = ResourceUrl.GetEmbeddedResourcePath(url); string extension = Path.GetExtension(embeddedResourcePath.Last <string>()).ToLower(); Stream stream = this.TryGetResourceWithFullPath(assembly, embeddedResourcePath); if (stream != null) { resourceHandler.RespondWith(stream, extension); } }