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!

     

     

     

     

    Add:  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 not know how to use the  LoadedEvent Handle Method.

    Friday, October 21, 2011 7:14 AM

Answers

  • Not sure why you want to instance the webbrowser class in a method. Don’t think there is anything special about the class, so it would be a local instance to the method. Once you leave scope, (return) you can’t reference it and would be subject to garbage collection.

     

    In your first example, you have a blocking message box, so it probably loads and works because you take time to click the message box in 7.1, 7.0 probably loads differently. The second example won’t work once you leave scope.

     

    If you want to control when it is created, then declare it globally, and initialize it in a method;

     

            // Constructor

            public MainPage()

            {

                InitializeComponent();

                InitWebBrowser();

            }

     

            WebBrowser MyWeb;   // declare

            void InitWebBrowser()

            {

                MyWeb = new WebBrowser();   // init

                LayoutRoot.Children.Add(MyWeb);

                MyWeb.Loaded +=new RoutedEventHandler(MyWeb_Loaded);    // add event handler

            }

     

            void MyWeb_Loaded(object sender, RoutedEventArgs e)

            {

                MyWeb.Navigate(new Uri("http://www.microsoft.com"));

            }

     

    Friday, October 21, 2011 6:05 PM
  • Hi,

    In general, the restriction on calling WebBrowser methods (including Navigate) is true for all versions; since loading the visual tree takes time, you might get lucky and it might happen to work if the visual tree happens to get loaded before you make the navigation call. But there is no guarantee that it will always work.  Therefore in order to have a reliable application, you always need to wait until the WebBrowser is loaded. The official MSDN content and explanation is here.

    Hope this helps,
    Mark
    Friday, October 21, 2011 6:35 PM
  • Thank you all very much!
    I''m a new, I'm not familiar with windows phone development. I always use a way you will never use in development which doesn't work in OS7.0 but works in OS7.1 .
    In fact, there is another way I can use to complete my work in OS7.0 .

    I have solved this problem by reading the content Mark Chamberlain suggested.
    Monday, October 24, 2011 3:45 AM

All replies

  • Not sure why you want to instance the webbrowser class in a method. Don’t think there is anything special about the class, so it would be a local instance to the method. Once you leave scope, (return) you can’t reference it and would be subject to garbage collection.

     

    In your first example, you have a blocking message box, so it probably loads and works because you take time to click the message box in 7.1, 7.0 probably loads differently. The second example won’t work once you leave scope.

     

    If you want to control when it is created, then declare it globally, and initialize it in a method;

     

            // Constructor

            public MainPage()

            {

                InitializeComponent();

                InitWebBrowser();

            }

     

            WebBrowser MyWeb;   // declare

            void InitWebBrowser()

            {

                MyWeb = new WebBrowser();   // init

                LayoutRoot.Children.Add(MyWeb);

                MyWeb.Loaded +=new RoutedEventHandler(MyWeb_Loaded);    // add event handler

            }

     

            void MyWeb_Loaded(object sender, RoutedEventArgs e)

            {

                MyWeb.Navigate(new Uri("http://www.microsoft.com"));

            }

     

    Friday, October 21, 2011 6:05 PM
  • Hi,

    In general, the restriction on calling WebBrowser methods (including Navigate) is true for all versions; since loading the visual tree takes time, you might get lucky and it might happen to work if the visual tree happens to get loaded before you make the navigation call. But there is no guarantee that it will always work.  Therefore in order to have a reliable application, you always need to wait until the WebBrowser is loaded. The official MSDN content and explanation is here.

    Hope this helps,
    Mark
    Friday, October 21, 2011 6:35 PM
  • Thank you all very much!
    I''m a new, I'm not familiar with windows phone development. I always use a way you will never use in development which doesn't work in OS7.0 but works in OS7.1 .
    In fact, there is another way I can use to complete my work in OS7.0 .

    I have solved this problem by reading the content Mark Chamberlain suggested.
    Monday, October 24, 2011 3:45 AM