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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20852204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:20:47  来源:igfitidea点击:

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

wpfvb.nettabcontrolcefsharp

提问by logic8

[disclaimer: I am new to Visual Basic.]

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

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

在 WPF 中,我有一个包含 2 个 TabItem 的 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 中的 url 时,它会在 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://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.

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

采纳答案by JSuar

Your code doesn't use Windows Forms' TabPagebut 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 Methodmight help in your situation.

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

回答by Budman1

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:

我遇到了同样的问题,但发现如果我使用标签页的名称并在表单加载代码上显示 (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 中对我不起作用。