using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using CefSharp; using CefSharp.Wpf; namespace Muchinfo.MTPClient.Delivery.Views { /// /// PrintWindow.xaml 的交互逻辑 /// public partial class PrintWindow : Window, IRequestHandler { private WebView _view; public PrintWindow(string url) { InitializeComponent(); CEF.Initialize(new Settings { LogSeverity = LogSeverity.Disable, PackLoadingDisabled = true }); BrowserSettings browserSetting = new BrowserSettings { ApplicationCacheDisabled = true, PageCacheDisabled = true }; _view = new WebView(string.Empty, browserSetting) { Address = url, RequestHandler = this, Background = Brushes.White }; // _view.RegisterJsObject("callbackObj", new CallbackObjectForJs()); _view.LoadCompleted += _view_LoadCompleted; PrintGrid.Children.Clear(); PrintGrid.Children.Insert(0, _view); this.Unloaded += WebPageViewer_Unloaded; } public bool GetAuthCredentials(IWebBrowser browser, bool isProxy, string host, int port, string realm, string scheme, ref string username, ref string password) { return false; } public bool GetDownloadHandler(IWebBrowser browser, string mimeType, string fileName, long contentLength, ref IDownloadHandler handler) { return false; } public bool OnBeforeBrowse(IWebBrowser browser, IRequest request, NavigationType naigationvType, bool isRedirect) { return false; } public bool OnBeforeResourceLoad(IWebBrowser browser, IRequestResponse requestResponse) { return false; } public void OnResourceResponse(IWebBrowser browser, string url, int status, string statusText, string mimeType, System.Net.WebHeaderCollection headers) { } private void WebPageViewer_Unloaded(object sender, RoutedEventArgs e) { ////资源清理 try { if (_view != null) { _view.CloseDevTools(); } } catch { } try { if (_view != null) { _view.Dispose(); } _view = null; } catch { } } private void _view_LoadCompleted(object sender, LoadCompletedEventArgs url) { if (_view != null) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { printButton.IsEnabled = true; })); } } public void View(string url) { if (_view.IsBrowserInitialized) { _view.Visibility = Visibility.Hidden; _view.Load(url); } } private void PrintButton_OnClick(object sender, RoutedEventArgs e) { if (_view != null) { _view.Print(); } } } }