【问题标题】:How would I call my Web Browser Navigate method in a different form?如何以不同的形式调用我的 Web Browser Navigate 方法?
【发布时间】:2013-11-05 05:03:35
【问题描述】:

我有两种形式。我的第一个表单是我的网络浏览器,第二个表单是我的历史记录表单。我希望用户能够从我的历史记录表单中打开网络浏览器中的历史记录链接。我的网络浏览器表单有一个导航方法,我用它来打开页面。我想在我的历史表单中使用这种方法。

这是我的代码。

Web 浏览器表单导航方法

 private void navigateURL(string curURL )
    {

        curURL = "http://" + curURL;
       // urlList.Add(curURL);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(curURL);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream pageStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(pageStream, Encoding.Default);
        string s = reader.ReadToEnd();
        webDisplay.Text = s;
        reader.Dispose();
        pageStream.Dispose();
        response.Close();

    }

我如何在我的网络浏览器类中调用我的导航方法

  private void homeButton_Click(object sender, EventArgs e)
    {
        GetHomePageURL();
        navigateURL(addressText);
    }

那么我该如何在第二种形式(历史)中调用这个方法?

【问题讨论】:

    标签: c# winforms xml-parsing browser


    【解决方案1】:

    我马上想到的两种方法...

    1. 提出一个 Web 浏览器表单监听的方法。每当用户选择他们想要导航到的历史条目时,您都需要声明该事件并在历史表单中引发它:

      // In the history form, declare event + event-handler delegate
      // Kind of abusing events here, you'd typically have the sender
      // and some sort of eventargs class you'd make...
      public delegate void NavigationRequestedEventHandler(string address);
      public event NavigationRequestedEventHandler NavigationRequested;
      
      // And where you handle the user selecting an history item to navigate to
      // you'd raise the event with the address they want to navigate to
      if (NavigationRequested != null)
          NavigationRequested(address);
      

      然后在网络浏览器表单中,您需要在创建历史表单时为该事件添加一个处理程序:

       // Subscribe to event
       this._historyForm = new HistoryForm()
       this._historyForm.NavigationRequested += HistoryForm_NavigationRequested;
      
      // And in the event handler you'd call your navigate method
      private void HistoryForm_NavigationRequested(string address)
      {
        navigateURL(address);
      }
      

      如果您要创建和丢弃多个历史表单,请确保删除您的处理程序 (_historyForm.NavigationRequested -= HistoryForm_NavigationRequested)。 这是个好习惯。

    2. 为历史表单提供对 Web 浏览器表单的引用。当您创建历史表单时,Web 浏览器表单将向其传递一个对自身的引用:new HistoryForm(Me)... 理想情况下,它会采用一个在其中定义了 Navigate 方法的接口。它会保存该引用并使用它来调用导航。

      IWebBrowser WebBrowserForm; // Has the method Navigate(string address)
      
      public HistoryForm(IWebBrowser webBrowser)
      {
          this.WebBrowserForm = webBrowser;
      }
      
      private void homeButton_Click(object sender, EventArgs e)
      {
          GetHomePageURL();
          WebBrowserForm.Navigate(address);
      }
      

    【讨论】:

    • 嘿杰夫,我不太明白你的第一个方法。 raise 方法是什么意思?
    • 与历史表单中的homeButton_Click 事件处理程序一样,主表单中也有一个historyForm_NavigationRequest 事件处理程序。您将在历史表单中声明事件,然后在创建历史表单后在主表单中添加处理程序。如有必要,我可能会添加更多代码,但最好阅读有关事件的教程...
    • 嘿 Jeff,homeButton_Click 事件不在我的历史记录中,而是在我的主要形式中。我举了一个例子,说明我如何在不同的事件中调用类中的导航方法。我理解将一个表单启动到另一个表单的需要,因为我已经设法解析表单之间的数据,但我仍然无法掌握我需要在historyForm_NavigationRequest 事件处理程序中放入的内容。我会在这里放什么代码?
    • 我已经大大扩展了事件部分...看看。
    • 非常感谢 Jeff 很抱歉之前没有提供反馈。但是您的解释在扩展后更有意义。我会进一步测试并让你知道。再次感谢。
    猜你喜欢
    • 2020-01-15
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多