WebBrowser不会在tabitem中显示

时间:2017-01-12 13:28:26

标签: c# wpf chromium-embedded

我尝试在c#,wpf和CefSharp上开发WebBrowser以获得经验。 我在WebBrowser中使用TabControl作为选项卡。 所以我将webbrowser添加到tabItem.Content但它没有显示出来。

public partial class MainWindow : Window
{
    ChromiumWebBrowser webBrowser;

    public MainWindow()
    {
        //Browser init
        CefSettings settings = new CefSettings();
        Cef.Initialize(settings);

        InitializeComponent();
        textboxURL.Text = "https://www.google.com";
        webBrowser = new ChromiumWebBrowser();
        webBrowser.Load(textboxURL.Text);
        tabItem1.Content = webBrowser;
    }

    //App Shutdown logic
    private void Exit(object sender, StartupEventArgs e)
    {
        Cef.Shutdown();
    }
}

这是XAML代码。如您所见,我使用DockPanel作为顶部栏,带有URL和按钮。 TabControl用于带浏览器的选项卡。现在我只使用一个webBrowser作为一个标签。

Window x:Class="WebBrowser.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WebBrowser"
    xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
    mc:Ignorable="d"
    Title="WebBrowser" Height="362" Width="699" WindowState="Maximized" WindowStartupLocation="CenterScreen">
<StackPanel HorizontalAlignment="Stretch" Margin="0,0,0,-1" Height="auto" VerticalAlignment="Stretch" >
    <DockPanel VerticalAlignment="Top">
        <Button x:Name="btnHome" Content="Home" Margin="2" DockPanel.Dock="Left"/>
        <Button x:Name="btnBack" Content="Back" Margin="2" DockPanel.Dock="Left"/>
        <Button x:Name="btnNext" Content="Next" Margin="2" DockPanel.Dock="Left"/>
        <Label x:Name="labelHttp" Content="HTTP" DockPanel.Dock="Left"/>
        <Button x:Name="btnMenu" Content="Menu" Margin="2" DockPanel.Dock="Right"/>
        <Button x:Name="btnGo" Content="Go" Margin="2" DockPanel.Dock="Right"/>
        <Button x:Name="btnRefresh" Content="Refresh" Margin="2" DockPanel.Dock="Right"/>
        <TextBox x:Name="textboxURL" Margin="2"/>
    </DockPanel>
    <TabControl x:Name="tabControl1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,5,0,0" Height="299">
        <TabItem Header="TabPage" Name="tabItem1">
        </TabItem>
    </TabControl>
</StackPanel>

2 个答案:

答案 0 :(得分:0)

好的,我尝试使用Address属性然后使用init浏览器并加载我的url! 所以答案是不要使用webBrowser.Load(textboxURL.Text);在新元素上但使用webBrowser.Address = textBoxURL.Text; 谢谢大家!

答案 1 :(得分:0)

-使用CefSharp,您可以使用此代码-

public ChromiumWebBrowser drv;
    string url = "https://www.example.com"; 
    public void syhmhfzaddtab()
    {
        TabPage tb = new TabPage();
        CefSettings settings = new CefSettings();
        Cef.Initialize(settings);

        drv = new ChromiumWebBrowser(url);
        drv.Parent = tb;
        tabControl1.Controls.Add(tb);
        drv = new ChromiumWebBrowser(url);
        drv.Dock = DockStyle.Fill;
    }

注意:%100工作代码