如何强制TabItem在加载时初始化内容? [英] How can I force a TabItem to initialize content on load?

查看:390
本文介绍了如何强制TabItem在加载时初始化内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

第一个TabItem包含一堆URL。

The first TabItem contains a bunch of URLs.

第二个TabItem由一个包含cefSharp webView的DockPanel组成(

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

当我单击tab1中的网址时,它将在tab2中包含的浏览器中加载一个页面... 但是,它仅适用于如果我先通过单击tab2来初始化浏览器。

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.

进行一些搜索后,看起来vb.net并未初始化TabItem,直到它变得可见为止。 (对吗?)

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)

编辑:

根据要求,下面是相关代码:

As requested, here is the relevant code:

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

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

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

这是我的代码背后 vb脚本:

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

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

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.

推荐答案

您的代码未使用Windows Forms的 TabPage ,但这也许仍然可以帮助

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


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

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.

通过MSDN论坛

此外,基于上述想法,您是否尝试过以下方法。

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

tab2.isEnabled = True
tab1.isEnabled = True

或:

tab2.Visibility = True
tab1.Visibility = True

此外, BeginInit方法在您遇到的情况中可能会有所帮助。

Also, the BeginInit Method might help in your situation.

这篇关于如何强制TabItem在加载时初始化内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆