[免责声明:我是 Visual Basic 新手。]
在 WPF 中,我有一个包含 2 个 TabItem 的 TabControl:
第一个 TabItem 包含一堆 URL。
第二个 TabItem 由一个 DockPanel 组成,其中包含一个 cefSharp webView(为 .net 嵌入的 chromium)
当我点击 tab1 中的 url 时,它会在 tab2 中包含的浏览器中加载一个页面... 但是,只有当我首先通过点击 tab2 初始化浏览器时它才有效。
经过一些搜索后,看起来 vb.net 不会初始化 TabItem 中的内容,直到它变得可见。 (对吗?)
所以,我的问题是,如何强制未选定的选项卡在后台加载时初始化其内容? (即,所以我不必先单击选项卡或切换到它)
编辑:
根据要求,以下是相关代码:
相关的 XAML 由一个名为“mainBox”的 DockPanel 组成
<DockPanel Name="mainBox" Width="Auto" Height="Auto" Background="#afe0ff" />
这是我的“代码隐藏”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
因此,在默认状态下,tab1 在启动时显示 - tab2 (web_view2) 上的浏览器不会初始化,直到我单击其选项卡或通过脚本更改到其选项卡。希望这能澄清一点。
最佳答案
您的代码不使用 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.
另外,基于上述想法,您是否尝试过以下操作。
tab2.isEnabled = True
tab1.isEnabled = True
或者:
tab2.Visibility = True
tab1.Visibility = True
此外,BeginInit Method可能对您的情况有所帮助。
关于wpf - 如何强制 TabItem 在加载时初始化内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20852204/