简体   繁体   English

使用Web浏览器控件构建WPF项目时出错

[英]Error while building WPF project with Web Browser control

In my application, I have a UserControl in which I use a WebBrowser control. 在我的应用程序中,我有一个使用WebBrowser控件的UserControl

The following is the code for the UserControl : 以下是UserControl的代码:

<UserControl x:Class="tool.ucMain"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="354" d:DesignWidth="440">
<Grid>
    <DockPanel>
        <WebBrowser Name="wbMap" DockPanel.Dock="Left" MinWidth="300"></WebBrowser>
        <DataGrid Name="grdSignals" DockPanel.Dock="Right" ItemsSource="{Binding}" AutoGenerateColumns="False">
        </DataGrid>
    </DockPanel>
</Grid>

I am using this UserControl in another UserControl which has a TabControl . 我使用这个UserControl在另一个UserControl具有TabControl This is the code for the other UserControl : 这是另一个UserControl的代码:

<Grid>
    <TabControl>
        <TabItem Name="tabMain" Header="Main">
            <uc:ucMain />
        </TabItem>
        <TabItem Name="tabDataExplorer" Header="Data Explorer"/>
    </TabControl>
</Grid>

The ucMain is the first UserControl . ucMain是第一个UserControl When I clean the project I can see the first user control. 当我清理项目时,我可以看到第一个用户控件。 But when I build/run it, it disappears and I get the error as shown in the following image. 但是当我构建/运行它时,它消失了,并且收到如下图所示的错误。 I get this error even in design view. 即使在设计视图中,我也会收到此错误。

在此处输入图片说明

I duplicated this behavior by creating a UserControl that contains the WebBrowser, and called that BrowserContainer. 我通过创建一个包含WebBrowser的UserControl来重复此行为,并将其命名为BrowserContainer。 Then, I created another UserControl called BrowserBrowserContainer and added my first UserControl to that. 然后,我创建了另一个名为BrowserBrowserContainer的UserControl,并向其中添加了我的第一个UserControl。 After that, I added the BrowserBrowserContainer UserControl to my MainWindow.xaml file for my test project. 之后,我将BrowserBrowserContainer UserControl添加到我的测试项目的MainWindow.xaml文件中。

When I give the WebBrowser a name, such as "Browser", and then add the following code to the constructor for BrowserContainer, I get the exact same behavior that you describe. 当给WebBrowser提供一个名称(例如“ Browser”),然后将以下代码添加到BrowserContainer的构造函数中时,我得到的行为与您描述的完全相同。

public BrowserContainer()
{
    InitializeComponent();
    Browser.Source = new Uri("c:\\thisfiledoesntexist");
}

Naturally, the solution is not to set the Source, or to set it to something that is valid at design time. 自然,解决方案是不设置Source或将其设置为在设计时有效的东西。 The parameterless constructor is called whether you're in design mode or not. 无论您是否处于设计模式下,都会调用无参数构造函数。

If you try to navigate to your local file you should always append file:/// in the front of your Uri path. 如果尝试导航到本地文件,则应始终在Uri路径的前面附加file:///

EDIT 编辑

I just tested it, I can see this error occurred in designer view of the Main Window. 我刚刚进行了测试,可以在主窗口的设计器视图中看到此错误。 I can see Visual Studio tried to trigger the Loaded Event of the UserControl. 我可以看到Visual Studio试图触发UserControl的Loaded事件。 Somehow the AppDomain.CurrentDomain.BaseDirectory returns the Visual Studio path. AppDomain.CurrentDomain.BaseDirectory以某种方式返回Visual Studio路径。

The best way to skip this error might be using DesignerProperties.GetIsInDesignMode() 跳过此错误的最佳方法可能是使用DesignerProperties.GetIsInDesignMode()

For example you can do the following to skip this error in designer view. 例如,您可以执行以下操作以在设计器视图中跳过此错误。

   private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        if (DesignerProperties.GetIsInDesignMode(this)) return;
        String appdir = System.IO.Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
        String mapPath = System.IO.Path.Combine(appdir, "MAP.html");
        ResultWebBrowser.Navigate(new Uri("file:///" + mapPath));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM