locked
“You cannot call WebBrowser methods until it is in the visual tree”------Error in windows phone 7 but not in windows phone 7.1 RRS feed

  • Question

  • I met a problem that I created a project as below in  windows phone OS 7 and in  windows phone OS 7.1, when run the project with OS 7,it produced a error:“InvalidOperationException was unhandled” notes:“You cannot call WebBrowser methods until it is in the visual tree” but run with OS 7.1 it didn't happen.

    I have found that in windows phone OS 7, if you create a webbrowser control with the code "new WebBrowser", you must add a loaded event handle method in your code which isn't needed in OS 7.1 .

    I’m so depressed that as a new learner, I don't know how to solve the problem that I need to create a WebBrowser control in a void method and call the WebBrowser's method navigate which produced a error as above.

    My code as below,I hope someone could help me. Thanks!

    • Moved by Annabella Luo Friday, October 21, 2011 6:36 AM off-topic (From:Windows Presentation Foundation (WPF))
    Thursday, October 20, 2011 9:42 AM

Answers

All replies

  • I don't think there is anything wrong in using the Loaded method.  Also, this should work in both v7 and v7.1.  Do make sure to unsubscribe from the Loaded handler in the method like this:

    private void MyWeb_OnLoaded(object sender, RoutedEventArgs e)
    {
        MyWeb.Loaded -= this.MyWeb_OnLoaded;
    
        MyWeb.Navigate(new Uri("http://www.google.com/"));
    }
    

    In .NET 4.5 (available in Win8 Preview), they finally made the web browsing experience more native to WPF.  It wouldn't surprise me if Win Phone 7.1 had some of these updates which allows the original code to work.

    Thursday, October 20, 2011 2:23 PM
  • Hi!

    Thank you for replying my question.

    But did you have noticed that the WebBrowser control "MyWeb" is created in the "public MainPage() {}"? I can't use it in the LoadedEvent Handle Method. If I create the WebBrowser control "MyWeb" in a global way, it will work. However, in my project, I have to create the WebBrowser control "MyWeb" in a method that makes me don't know how to use the  LoadedEvent Handle Method.

     

    Friday, October 21, 2011 2:15 AM
  • Hi fisherforever,

    Thank you for your post.

    According to your description and code, I know your issue is about Windows Phone development. So I moved you issue.

    And If you have any else issue about Window Phone, I suggest you post your issue here:

    http://forums.create.msdn.com/forums/

    Thank you for your understanding.

    Have a nice day.


    Annabella Luo [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Friday, October 21, 2011 5:52 AM
  • Thank you for moving my issue. And I'm sorry to inconvenience you. When I was posting the issue, I did not find the correct forum.
    Friday, October 21, 2011 7:03 AM