簡體   English   中英

WPF屏幕外WebBrowser控件-頁面加載中的Javascript不起作用

[英]WPF Off-Screen WebBrowser Control - Javascript on pageload not working

我正在嘗試使用wpf中的WebBrowser控件導航到一個aspx。 該頁面上有一個JavaScript onload。 如果我將控件放在網格上並使控件的可見性變為折疊或隱藏,則Javascript調用將起作用,但我只希望屏幕外調用。 有沒有辦法做到這一點或不可能的事情?

ASPX:

 <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript"> function OnloadJSCall() { window.PageMethods.Docall(onSuccess, onFailure); } function onSuccess(result) { } function onFailure(error) { } </script> </head> <body> <form id="form1" runat="server"> <div><asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager> <script language="JavaScript"> OnloadJSCall();</script> </div> </form> </body> </html> 

 public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } [System.Web.Services.WebMethod] public static string Docall() { using (var fs = new FileStream("C:\\\\AD\\\\Test1111.txt", FileMode.Append, FileAccess.Write)) using (var sw = new StreamWriter(fs)) { sw.WriteLine(System.DateTime.Now); } return "Done"; } } 

WPF應用

 <Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> </Grid> </Window> 

MainWindow.xaml.cs

  public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var webBrowserControl = new WebBrowser();
            webBrowserControl.Navigate("http://localhost:53288/WebForm1.aspx");
            webBrowserControl.Navigated += webBrowserControlOnNavigated;
        }

        private void webBrowserControlOnNavigated(object sender, NavigationEventArgs navigationEventArgs)
        {
        }
    }

好吧,我得到了答案。 如果沒有ui組件,則Javascript將無法運行。 因此,將控件添加到表單中就可以了!!

        var newForm =new Form();

        var webBrowserControl = new System.Windows.Forms.WebBrowser();
        webBrowserControl.Navigate("http://localhost:53288/WebForm1.aspx");

        newForm.Controls.Add(webBrowserControl);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM