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);
3
  • 3
    Have you caught the TargetInvocationException and looked at the InnerException? Take a look at stackoverflow.com/questions/2658908/… for help. Commented Sep 18, 2012 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? Commented Sep 18, 2012 at 20:18
  • 1
    webBrowser1.Source is probably null. Regardless, you should move this to Loading and I'll advise in an answer. Commented Sep 18, 2012 at 21:20

1 Answer 1

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();
            // ...
        }
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.