PrintWindow.xaml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Shapes;
  13. using CefSharp;
  14. using CefSharp.Wpf;
  15. namespace Muchinfo.MTPClient.Delivery.Views
  16. {
  17. /// <summary>
  18. /// PrintWindow.xaml 的交互逻辑
  19. /// </summary>
  20. public partial class PrintWindow : Window, IRequestHandler
  21. {
  22. private WebView _view;
  23. public PrintWindow(string url)
  24. {
  25. InitializeComponent();
  26. CEF.Initialize(new Settings { LogSeverity = LogSeverity.Disable, PackLoadingDisabled = true });
  27. BrowserSettings browserSetting = new BrowserSettings { ApplicationCacheDisabled = true, PageCacheDisabled = true };
  28. _view = new WebView(string.Empty, browserSetting)
  29. {
  30. Address = url,
  31. RequestHandler = this,
  32. Background = Brushes.White
  33. };
  34. // _view.RegisterJsObject("callbackObj", new CallbackObjectForJs());
  35. _view.LoadCompleted += _view_LoadCompleted;
  36. PrintGrid.Children.Clear();
  37. PrintGrid.Children.Insert(0, _view);
  38. this.Unloaded += WebPageViewer_Unloaded;
  39. }
  40. public bool GetAuthCredentials(IWebBrowser browser, bool isProxy, string host, int port, string realm, string scheme, ref string username, ref string password)
  41. {
  42. return false;
  43. }
  44. public bool GetDownloadHandler(IWebBrowser browser, string mimeType, string fileName, long contentLength, ref IDownloadHandler handler)
  45. {
  46. return false;
  47. }
  48. public bool OnBeforeBrowse(IWebBrowser browser, IRequest request, NavigationType naigationvType, bool isRedirect)
  49. {
  50. return false;
  51. }
  52. public bool OnBeforeResourceLoad(IWebBrowser browser, IRequestResponse requestResponse)
  53. {
  54. return false;
  55. }
  56. public void OnResourceResponse(IWebBrowser browser, string url, int status, string statusText, string mimeType, System.Net.WebHeaderCollection headers)
  57. {
  58. }
  59. private void WebPageViewer_Unloaded(object sender, RoutedEventArgs e)
  60. {
  61. ////资源清理
  62. try
  63. {
  64. if (_view != null)
  65. {
  66. _view.CloseDevTools();
  67. }
  68. }
  69. catch { }
  70. try
  71. {
  72. if (_view != null)
  73. {
  74. _view.Dispose();
  75. }
  76. _view = null;
  77. }
  78. catch { }
  79. }
  80. private void _view_LoadCompleted(object sender, LoadCompletedEventArgs url)
  81. {
  82. if (_view != null)
  83. {
  84. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  85. {
  86. printButton.IsEnabled = true;
  87. }));
  88. }
  89. }
  90. public void View(string url)
  91. {
  92. if (_view.IsBrowserInitialized)
  93. {
  94. _view.Visibility = Visibility.Hidden;
  95. _view.Load(url);
  96. }
  97. }
  98. private void PrintButton_OnClick(object sender, RoutedEventArgs e)
  99. {
  100. if (_view != null)
  101. {
  102. _view.Print();
  103. }
  104. }
  105. }
  106. }