Click here to Skip to main content
15,893,161 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: WPF ComboBox With User Control(This may not work.) Pin
David C# Hobbyist.30-Mar-13 10:52
professionalDavid C# Hobbyist.30-Mar-13 10:52 
GeneralRe: WPF ComboBox With User Control(This may not work.) Pin
Kevin Marois30-Mar-13 11:09
professionalKevin Marois30-Mar-13 11:09 
GeneralRe: WPF ComboBox With User Control(This may not work.) Pin
David C# Hobbyist.30-Mar-13 11:24
professionalDavid C# Hobbyist.30-Mar-13 11:24 
AnswerRe: WPF ComboBox With User Control Pin
SledgeHammer0130-Mar-13 13:19
SledgeHammer0130-Mar-13 13:19 
GeneralWindows 8 WPF Theme? Pin
Member 982361928-Mar-13 17:20
Member 982361928-Mar-13 17:20 
GeneralRe: Windows 8 WPF Theme? Pin
Meshack Musundi4-Apr-13 7:48
professionalMeshack Musundi4-Apr-13 7:48 
QuestionC# WPF Accessing parameter of chosen data grid row Pin
johnyjj228-Mar-13 9:55
johnyjj228-Mar-13 9:55 
QuestionExtending WPF WebBrowser Control Pin
Coxianuk28-Mar-13 6:48
Coxianuk28-Mar-13 6:48 
I am trying to provide a UserControl that wrappers up the WebBrowser control and provides the additional events raised on the DWebBrowserEvents_Event and DWebBrowserEvents2_Event interfaces.

I have a class that works fine when only one extended browser control is in the xaml hwever when I have 2 the second one works but the first is failing to show the page its pointed at.

The user control code is as follows

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace CustomControls
{
    public partial class ExtWebBrowser : UserControl
    {
        private static WebBrowser baseBrowser;
        private SHDocVw.DWebBrowserEvents_Event wbEvents;
        private SHDocVw.DWebBrowserEvents2_Event wbEvents2;
        private SHDocVw.IWebBrowser2 myWebBrowser2;

        static ExtWebBrowser()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ExtWebBrowser), new FrameworkPropertyMetadata(typeof(ExtWebBrowser)));
        }

        public ExtWebBrowser()
        {
        }

        #region Dependency Properties

        #region UrlProperty

        public Uri Url
        {
            get { 
                return (Uri)GetValue(UrlProperty); 
            }
            set { 
                SetValue(UrlProperty, value); 
            }
        }

        // Using a DependencyProperty as the backing store for Source.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty UrlProperty =
            DependencyProperty.Register("Url", typeof(Uri), typeof(ExtWebBrowser), new UIPropertyMetadata(new Uri("about:blank"), UrlChanged));

        private static void UrlChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            if (baseBrowser != null)
            {
                baseBrowser.Source = e.NewValue as Uri;
            }
        }

        #endregion

        #region ScriptingObjectProperty

        public object ScriptingObject
        {
            get { 
                return (object)GetValue(ScriptingObjectProperty); }
            set { 
                SetValue(ScriptingObjectProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ScriptingObject.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ScriptingObjectProperty =
            DependencyProperty.Register("ScriptingObject", typeof(object), typeof(ExtWebBrowser), new UIPropertyMetadata(null, ScriptingObjectChanged));

        private static void ScriptingObjectChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            if (baseBrowser != null)
            {
                baseBrowser.ObjectForScripting = e.NewValue;
            }
        }

        #endregion ScriptingObjectProperty

        #endregion Dependecy Properties

        #region Member fields
        private bool _firstLoad = false;

        #endregion


        #region Internals

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            baseBrowser = base.Template.FindName("PART_baseBrowser", this) as WebBrowser;
            if (baseBrowser != null)
            {
                _firstLoad = true;
                baseBrowser.LoadCompleted += new LoadCompletedEventHandler(baseBrowser_LoadCompleted);
                baseBrowser.ObjectForScripting = ScriptingObject;
                baseBrowser.Source = new Uri("about:blank");
            }
        }

        void baseBrowser_LoadCompleted(object sender, NavigationEventArgs e)
        {
            if (_firstLoad)
            {
                Guid SID_SWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
                IServiceProvider serviceProvider = (IServiceProvider)baseBrowser.Document;

                Guid serviceGuid = SID_SWebBrowserApp;

                Guid iid = typeof(SHDocVw.IWebBrowser2).GUID;
                //Here we will get a reference to the IWebBrowser2 interface
                myWebBrowser2 = (SHDocVw.IWebBrowser2)serviceProvider.QueryService(ref serviceGuid, ref iid);
                //To hook events we just need to do these casts
                wbEvents = (SHDocVw.DWebBrowserEvents_Event)myWebBrowser2;
                wbEvents2 = (SHDocVw.DWebBrowserEvents2_Event)myWebBrowser2;

                //HookupExtendedEvents();

                baseBrowser.Source = Url;
                _firstLoad = false;
            }
        }


        #endregion Internals
    }

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid("6d5140c1-7436-11ce-8034-00aa006009fa")]
    internal interface IServiceProvider
    {
       [return: MarshalAs(UnmanagedType.IUnknown)]

       object QueryService(ref Guid guidService, ref Guid riid);

    }
}


The xaml is as follows

<Window x:Class="UCA.ChordiantWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:custom="clr-namespace:CustomControls;assembly=Uca.CustomControls"
        Title="ChordiantWindow" Height="300" Width="300" SizeToContent="WidthAndHeight" ResizeMode="NoResize">
    <Window.Resources>
        <Style TargetType="{x:Type custom:ExtWebBrowser}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type custom:ExtWebBrowser}">
                        <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                            <Grid>
                                <Button>Hello</Button>
                                <WebBrowser x:Name="PART_baseBrowser" />
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <custom:ExtWebBrowser Url="http://www.amazon.co.uk" />
            <custom:ExtWebBrowser Url="http://www.microsoft.com" />
        </StackPanel>
    </Grid>
</Window>


What have I got wrong?

EDIT
Thought it might be the dreaded WebBrowser control itself however the page works if I use to Webrowser contols rather than the extended one.

Also It looks like the Url DP gets shared as another page with the control in seems to change its content in-line with changes to the window?
Ian Cox
----------------------------------------------------------
The more simple it looks, the harder it is to do!


modified 28-Mar-13 14:22pm.

AnswerRe: Extending WPF WebBrowser Control Pin
Richard Deeming28-Mar-13 9:19
mveRichard Deeming28-Mar-13 9:19 
GeneralRe: Extending WPF WebBrowser Control Pin
Coxianuk28-Mar-13 10:26
Coxianuk28-Mar-13 10:26 
GeneralRe: Extending WPF WebBrowser Control Pin
David C# Hobbyist.28-Mar-13 10:52
professionalDavid C# Hobbyist.28-Mar-13 10:52 
QuestionWindows Phone App Development using C# 2010 Pin
TwiztedFreek26-Mar-13 18:16
TwiztedFreek26-Mar-13 18:16 
QuestionRe: Windows Phone App Development using C# 2010 Pin
Richard MacCutchan26-Mar-13 23:00
mveRichard MacCutchan26-Mar-13 23:00 
AnswerRe: Windows Phone App Development using C# 2010 Pin
Abhinav S27-Mar-13 18:02
Abhinav S27-Mar-13 18:02 
QuestionLooking for a windows phone 7.1 XNA programmer Pin
Member 955005525-Mar-13 23:09
Member 955005525-Mar-13 23:09 
AnswerRe: Looking for a windows phone 7.1 XNA programmer Pin
Richard MacCutchan26-Mar-13 0:09
mveRichard MacCutchan26-Mar-13 0:09 
AnswerRe: Looking for a windows phone 7.1 XNA programmer Pin
Marco Bertschi26-Mar-13 1:15
protectorMarco Bertschi26-Mar-13 1:15 
QuestionCode-generated buttons in Stackpanel: how to handle events? Pin
Dirk.Bock22-Mar-13 12:24
Dirk.Bock22-Mar-13 12:24 
AnswerRe: Code-generated buttons in Stackpanel: how to handle events? Pin
Mycroft Holmes22-Mar-13 14:14
professionalMycroft Holmes22-Mar-13 14:14 
AnswerRe: Code-generated buttons in Stackpanel: how to handle events? Pin
Gerry Schmitz22-Mar-13 14:51
mveGerry Schmitz22-Mar-13 14:51 
QuestionMVVM Tasks and UI Updating Pin
cjb11022-Mar-13 1:44
cjb11022-Mar-13 1:44 
AnswerRe: MVVM Tasks and UI Updating Pin
Matt T Heffron22-Mar-13 10:25
professionalMatt T Heffron22-Mar-13 10:25 
GeneralRe: MVVM Tasks and UI Updating Pin
cjb11024-Mar-13 22:37
cjb11024-Mar-13 22:37 
GeneralRe: MVVM Tasks and UI Updating Pin
cjb11025-Mar-13 1:56
cjb11025-Mar-13 1:56 
GeneralRe: MVVM Tasks and UI Updating Pin
cjb11025-Mar-13 4:45
cjb11025-Mar-13 4:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.