How can I force a TabItem to initialize content on load?

Viewed 7650

[disclaimer: I am new to Visual Basic.]

In a WPF, I have a TabControl containing 2 TabItems:

The first TabItem contains a bunch of URLs.

The second TabItem consists of a DockPanel that contains a cefSharp webView (chromium embedded for .net)

When I click on a url in tab1 it loads a page in the browser contained in tab2... But, it only works if I have initialized the browser first by clicking on tab2.

After doing some searching, it looks like vb.net doesn't initialize the content in a TabItem until it becomes visible. (right?)

So, my question is, how can I force a non-selected tab to initialize its content on load, in the background? (ie. so I don't have to click on the tab or switch to it first)

EDIT:

As requested, here is the relevant code:

The relevant XAML consists of a single DockPanel named "mainBox"

<DockPanel Name="mainBox" Width="Auto" Height="Auto" Background="#afe0ff" />

And here is my "code behind" vb script:

Class MainWindow : Implements ILifeSpanHandler, IRequestHandler

    Shared web_view1 As CefSharp.Wpf.WebView
    Shared web_view2 As CefSharp.Wpf.WebView

    Public Sub init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Loaded

    'This is in a DockPanel created on the xaml named mainBox

        ' set up tabControl:
        Dim browserTabs As New TabControl()
        browserTabs.BorderThickness = Nothing

        Dim tab1 As New TabItem()
        tab1.Header = "My Tab"

        Dim tab2 As New TabItem()
        tab2.Header = "Browser"

        Dim tab1Content As New DockPanel()
        Dim tab2Content As New DockPanel()

        tab1.Content = tab1Content
        tab2.Content = tab2Content

        browserTabs.Items.Add(tab1)
        browserTabs.Items.Add(tab2)

        mainBox.Children.Add(browserTabs)

        ' set up browsers:
        Dim settings As New CefSharp.Settings()
        settings.PackLoadingDisabled = True

        If CEF.Initialize(settings) Then

            web_view1 = New CefSharp.Wpf.WebView()
            web_view1.Name = "myTabPage"
            web_view1.Address = "http://stackoverflow.com/"

            web_view2 = New CefSharp.Wpf.WebView()
            web_view2.Name = "browserPage"
            web_view2.Address = "https://www.google.com"
            web_view2.LifeSpanHandler = Me
            web_view2.RequestHandler = Me

            AddHandler web_view2.PropertyChanged, AddressOf web2PropChanged

            tab1Content.Children.Add(web_view1)
            tab2Content.Children.Add(web_view2)

        End If

    End Sub
End Class

So, in its default state, tab1 is showing at start up -- the browser on tab2 (web_view2) won't initialize until I click its tab or change to its tab via script. Hope this clears it up a bit.

2 Answers
Related