Sloanstarr
Sloanstarr

Reputation: 11

C# WebBrowser - Help Adding Flags to Navigate Method

I'm not the greatest with COM objects but, I have a need to extend the WebBrowser control to support flags in the navigate method (Specifically to prevent reading/writing from cache).

From what I gather I'll need to implement IWebBrowser2 to some extent. Can I just implement the Navigate method or do I need to define all methods in the interface?

I've found some examples for attaching/detaching an event sink to extend the events of the web browser but very little around the actual Methods.

Can I use the underling ActiveXInstance of the WebBrowser control? If I create a class that implements IWebBrowser2::Navigate, and cast a variable to it that class, assigning the WebBrowser control ActiveXInstance then attempt to Navigate I get a COM exception HRESULT E_FAIL

I found this but not sure if the underlying control is still ShDocVw as I didn't see it in my COM objects (Target FW .Net 3.5): Web Browser to handle pop ups within the application

internal ShDocVw.WebBrowser ActiveXWebBrowser { get; private set; }`

new public void Navigate(string url)
{
    this.Navigate(url, axNativeMethods.WebBrowserNavigateFlags.NoReadFromCache | axNativeMethods.WebBrowserNavigateFlags.NoWriteToCache, string.Empty, new byte[] { }, string.Empty);
}

public void Navigate(string url, axNativeMethods.WebBrowserNavigateFlags flags, string targetFrameName, byte[] postData, string headers)
{

    this.ActiveXWebBrowser = (ShDocVw.WebBrowser)this.ActiveXInstance;`

    object flagsObj = (flags == axNativeMethods.WebBrowserNavigateFlags.None) ? null : (object)flags;
    object targetFrameNameObj = targetFrameName;
    object headersObj = headers;
    object postDataObj = postData;

    ActiveXWebBrowser.Navigate(url, ref flagsObj, ref targetFrameNameObj, ref postDataObj, ref headersObj);
}

Upvotes: 1

Views: 1113

Answers (1)

nerdshark
nerdshark

Reputation: 1

This is a perfect use case for extension methods. Basically, they allow you to define methods in a static class that appear to be bound directly to the type being extended, and which behave much like public instance methods on the type, except that only public or internal (in the case of friend assemblies or the extension class and extended class being in the same assembly) are available.

Here's an example snippet:

public static class WebBrowserExtensions
{
    public static void Navigate(this IWebBrowser2 browser, string url)
    {
        browser.Navigate(url, /* fill in arguments as necessary*/)
    }
}

Upvotes: 0

Related Questions