2

The problem is: When I remove the first message box line, my program doesn't run and throws "Exception has been thrown by the target of an invocation" on the if statement line. However, when I leave the messagebox there, it runs fine. Can someone explain to me why this is happening and what I can do to fix it? I'm fairly new to WPF by the way, any help would be appreciated.

public BrowserMode() {

       InitializeComponent();

       MessageBox.Show("Entering Browser Mode");
       if (webBrowser1.Source.Scheme == "http")
       {
           //cancel navigation
           //this.NavigationService.Navigating += new NavigatingCancelEventHandler(Cancel_Navigation);

           qd = new QuestionData();

           // code where stuff happens
           var url = webBrowser1.Source;
           HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

           // from h.RequestUri = "webcam://submit?question_id=45"
           var parseUrl = request.RequestUri; //the uri that responded to the request.
           MessageBox.Show("The requested URI is: " + parseUrl);
Sixers17
  • 592
  • 2
  • 5
  • 20
  • 3
    Have you caught the TargetInvocationException and looked at the InnerException? Take a look at http://stackoverflow.com/questions/2658908/why-is-targetinvocationexception-treated-as-uncaught-by-the-ide for help. – user7116 Sep 18 '12 at 18:30
  • Okay so I commented out the MessageBox.show("enter browsermode) code and surrounded the code block by try/catch blocks. The message is saying "Object reference is not set to an instance of an object" on the next line (if statement). Why? – Sixers17 Sep 18 '12 at 20:18
  • 1
    `webBrowser1.Source` is probably `null`. Regardless, you should move this to `Loading` and I'll advise in an answer. – user7116 Sep 18 '12 at 21:20

1 Answers1

2

This sort of work is not suited for a constructor and should be moved out until after the WebBrowser is fully loaded. You have two options:

  1. Hook Control.Loaded and perform this behavior there.

    public BrowserMode()
    {
        InitializeComponent();
    
        this.Loaded += BroswerMode_Loaded;
    }
    
    void BrowserMode_Loaded(object sender, EventArgs e)
    {
        if (webBrowser1.Source != null
         && webBrowser1.Source.Scheme == "http")
        {
            qd = new QuestionData();
            // ...
        }
    }
    
  2. Hook WebBrowser.Navigating and perform this behavior there.

    public BrowserMode()
    {
        InitializeComponent();
    
        this.webBrowser1.Navigating += WebBrowser_Navigating;
    }    
    
    void WebBrowser_Navigating(object sender, NavigatingCancelEventArgs e)
    {
        if (e.Uri.Scheme == "http")
        {
            qd = new QuestionData();
            // ...
        }
    }
    
user7116
  • 63,008
  • 17
  • 141
  • 172
  • Hey. I did the loaded method and its not throwing target exception but it's still saying source is null and won't enter the code block. I tried explicitly setting the source property to a uri but doesnt work. I'm assuming its null because the page hasn't loaded, however, what's the work around with this if I'm setting the source in the .xaml page? – Sixers17 Sep 19 '12 at 23:20