using AksWebBrowser; using AKSWebBrowser.Commen; using CPF; using CPF.Animation; using CPF.Cef; using CPF.Charts; using CPF.Controls; using CPF.Drawing; using CPF.Platform; using CPF.Shapes; using CPF.Styling; using CPF.Svg; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AKS.EnterpriseLibrary.WebBrowser { public class FrmMain : Window { protected override void InitializeComponent() { LoadStyleFile("res://AksWebBrowser/StyleSheet.css"); //加载样式文件,文件需要设置为内嵌资源 Title = "控申业务专用浏览器"; CanResize = false; ShowInTaskbar = true; WindowState = WindowState.Maximized; Children.Add( new Border { Width = "100%", Height = "100%", Child = new Panel { Size = SizeField.Fill, Children = { new CusWebBrowser { PresenterFor = this, Name = nameof(webBrowser), Bindings = { { nameof(CusWebBrowser.Title), "Title", this, BindingMode.OneWayToSource }, }, MarginTop=0, MarginLeft=0, MarginRight=0, MarginBottom=0, }, } } } ); } private TextBox textBox; private CusWebBrowser webBrowser; protected override async void OnInitialized() { //窗体大小 this.Width = 1080; this.Height = 1920; //SetTaskStatus.Hidetask(); base.OnInitialized(); webBrowser = FindPresenterByName(nameof(webBrowser)); textBox = FindPresenterByName(nameof(textBox)); webBrowser.CusRequest.CusResquestEvent += CusRequest_CusResquestEvent; //浏览器大小 webBrowser.Width = 1080; webBrowser.Height = 1920; webBrowser.Url = "http://192.168.0.57:5173/"; //webBrowser.Url = Application.StartupPath + @"\html\index.html"; //开发者工具暂时只能支持Windows //webBrowser.ShowDev(); //SetTaskStatus.Showtask(); webBrowser.LoadEnd += WebBrowser_LoadEnd; this.Closing += MainWindow_Closing; } //关闭事件 private void MainWindow_Closing(object sender, ClosingEventArgs e) { MainModel.KillProcessByName("AksWebBrowser"); new MainModel().CLoseCOM(); } bool showDev = true; private void WebBrowser_LoadEnd(object sender, LoadEndEventArgs e) { if (!showDev) { showDev = true; webBrowser.ShowDev(); } } public void Writelog(string str, string dirName = @"logs") { try { if (string.IsNullOrEmpty(str)) return; var baseDir = AppDomain.CurrentDomain.BaseDirectory + dirName; if (!Directory.Exists(baseDir)) { Directory.CreateDirectory(baseDir); } string filePath = System.IO.Path.Combine(baseDir, DateTime.Now.ToString("yyyy-MM-dd") + "_log.txt"); using (StreamWriter sw = File.AppendText(filePath)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "[" + str + "]" + "\r\n"); sw.Flush(); } } catch { } } private void CusRequest_CusResquestEvent(CefPostData postData, CefRequest request) { string postParams = string.Empty; if (postData != null) { //取提交的参数 CefPostData cefPostData = postData; var elements = cefPostData.GetElements(); foreach (var element in elements) { if (element.ElementType == CefPostDataElementType.Bytes) { var bytes = element.GetBytes(); postParams = Encoding.UTF8.GetString(bytes); Console.WriteLine("请求参数:" + postParams); } } } Console.WriteLine("请求地址:" + request.Url.ToString()); } //调用JS内的JS方法 async void InvokeJS(CpfObject obj, RoutedEventArgs eventArgs) { var r = await webBrowser.ExecuteJavaScript("callback('调用绑定到JS里的C#方法')"); } } }