如何强制TabItem在加载时初始化内容?

时间:2022-05-22 22:41:40

[disclaimer: I am new to Visual Basic.]

[免责声明:我是Visual Basic的新手。]

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

在WPF中,我有一个包含2个TabItems的TabControl:

The first TabItem contains a bunch of URLs.

第一个TabItem包含一堆URL。

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

第二个TabItem包含一个DockPanel,它包含一个cefSharp webView(为.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.

当我点击tab1中的网址时,它会在tab2中包含的浏览器中加载一个页面...但是,只有在我通过单击tab2初始化浏览器时它才有效。

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

在进行一些搜索之后,看起来vb.net不会初始化TabItem中的内容,直到它变得可见。 (对?)

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"

相关的XAML由一个名为“mainBox”的DockPanel组成

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

And here is my "code behind" vb script:

这是我的“代码隐藏”vb脚本:

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://*.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.

因此,在默认状态下,tab1在启动时显示 - tab2(web_view2)上的浏览器将不会初始化,直到我单击其选项卡或通过脚本更改为其选项卡。希望这有点清除它。

2 个解决方案

#1


5  

Your code doesn't use Windows Forms' TabPage but perhaps this might still help

您的代码不使用Windows窗体的TabPage,但也许这可能仍然有用

As we know, Controls contained in a TabPage are not created until the tab page is shown, and any data bindings in these controls are not activated until the tab page is shown. This is by design and you can call TabPage.Show() method one by one as a workaround.

我们知道,在显示标签页之前不会创建TabPage中包含的控件,并且在显示标签页之前不会激活这些控件中的任何数据绑定。这是设计使然,您可以逐个调用TabPage.Show()方法作为解决方法。

via MSDN forums

通过MSDN论坛

Also, based on the above idea, have you tried the following.

另外,基于上述想法,您是否尝试过以下操作。

tab2.isEnabled = True
tab1.isEnabled = True

Or:

tab2.Visibility = True
tab1.Visibility = True

Also, the BeginInit Method might help in your situation.

此外,BeginInit方法可能对您的情况有所帮助。

#2


2  

I had the same problem but found it initializes if I use the name of the tabpage and show (tabName1.show()) on the form load code for each page ending with the one I want displayed:

我有同样的问题,但发现如果我使用tabpage的名称并在表单加载代码上显示(tabName1.show()),每个页面以我想要显示的那个结尾,则初始化:

tabPage2.show()
tabPage3.show()
tabPage4.show()
tabPage5.show()
tabPage1.show()

It ripples thru and you never see the pages changing but it works. The other suggestions didn't work for me in Visual Studio 2010.

它涟漪通过,你永远不会看到页面改变,但它的工作原理。其他建议在Visual Studio 2010中对我不起作用。

#1


5  

Your code doesn't use Windows Forms' TabPage but perhaps this might still help

您的代码不使用Windows窗体的TabPage,但也许这可能仍然有用

As we know, Controls contained in a TabPage are not created until the tab page is shown, and any data bindings in these controls are not activated until the tab page is shown. This is by design and you can call TabPage.Show() method one by one as a workaround.

我们知道,在显示标签页之前不会创建TabPage中包含的控件,并且在显示标签页之前不会激活这些控件中的任何数据绑定。这是设计使然,您可以逐个调用TabPage.Show()方法作为解决方法。

via MSDN forums

通过MSDN论坛

Also, based on the above idea, have you tried the following.

另外,基于上述想法,您是否尝试过以下操作。

tab2.isEnabled = True
tab1.isEnabled = True

Or:

tab2.Visibility = True
tab1.Visibility = True

Also, the BeginInit Method might help in your situation.

此外,BeginInit方法可能对您的情况有所帮助。

#2


2  

I had the same problem but found it initializes if I use the name of the tabpage and show (tabName1.show()) on the form load code for each page ending with the one I want displayed:

我有同样的问题,但发现如果我使用tabpage的名称并在表单加载代码上显示(tabName1.show()),每个页面以我想要显示的那个结尾,则初始化:

tabPage2.show()
tabPage3.show()
tabPage4.show()
tabPage5.show()
tabPage1.show()

It ripples thru and you never see the pages changing but it works. The other suggestions didn't work for me in Visual Studio 2010.

它涟漪通过,你永远不会看到页面改变,但它的工作原理。其他建议在Visual Studio 2010中对我不起作用。