2016-02-13 36 views
0

我将web浏览器控件从工具箱拖放到设计器中。为什么当创建一个wpf项目添加webbrowser设计师我得到的错误在xmal文件?

这是MainWindow.xmal

<Window x:Class="WPFWebBrowser.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="My Web Browser" WindowState="Normal" Loaded="Window_Loaded" WindowStyle="ThreeDBorderWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="529" Width="731"> 
    <Grid> 
     <Button Content="&lt;&lt;" Height="23" HorizontalAlignment="Left" Margin="10,5,0,0" Name="MyBack" VerticalAlignment="Top" Width="25" ToolTip="Backword" Click="MyBack_Click" /> 
     <WebBrowser Height="445" HorizontalAlignment="Left" Margin="10,33,0,0" Name="MyWebBrowser" VerticalAlignment="Top" Width="687" LoadCompleted="MyWebBrowser_LoadCompleted" /> 
     <TextBox Height="23" Margin="103,5,12,0" Name="MyTextBox" VerticalAlignment="Top" /> 
     <Button Content="|&gt;" Height="23" HorizontalAlignment="Left" Margin="41,5,0,0" Name="MyGo" VerticalAlignment="Top" Width="25" ToolTip="Go" Click="MyGo_Click" /> 
     <Button Content="&gt;&gt;" Height="23" HorizontalAlignment="Right" Margin="0,5,612,0" Name="MyForward" VerticalAlignment="Top" Width="25" ToolTip="Forward" Click="MyForward_Click" /> 
    </Grid> 
</Window> 

的代码,我有两个问题。

首先我没有运行时代码中的Window_Loaded事件。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace WPFWebBrowser 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 
    } 
} 

的第二个问题是,在MainWindow.xmal我得到5个错误

错误1 “WPFWebBrowser.MainWindow”不包含关于“Window_Loaded”的定义和没有扩展方法“Window_Loaded '接受型的第一参数‘WPFWebBrowser.MainWindow’可以找到(是否缺少using指令或程序集引用?)

错误2 ‘WPFWebBrowser.MainWindow’不包含用于定义‘MyBack_Click’和没有扩展方法'MyBack_Click'接受第一个理由型的T“WPFWebBrowser.MainWindow”可以找到(是否缺少using指令或程序集引用?)

错误3 “WPFWebBrowser.MainWindow”不包含关于“MyWebBrowser_LoadCompleted”的定义和没有扩展方法“ MyWebBrowser_LoadCompleted”接受型的第一参数‘WPFWebBrowser.MainWindow’可以找到(是否缺少using指令或程序集引用?)

错误4 ‘WPFWebBrowser.MainWindow’不包含用于定义‘MyGo_Click’并且没有找到接受类型'WPFWebBrowser.MainWindow'的第一个参数的扩展方法'MyGo_Click'(可以找到缺少使用指令或程序集引用吗?)

错误5 'WPFWebBrowser.MainWindow'没有包含'MyForward_Click'的定义,也没有找到接受'WPFWebBrowser.MainWindow'类型的第一个参数的扩展方法'MyForward_Click'(你是否缺少using指令或一个程序集的参考?)

我错过了一个dll我应该参考我的项目?

PresentationCore DLL已被引用。 同PresentationFramework和WindowsBase

+0

我不熟悉WPF,但我相信错误rs告诉你需要在你的代码中实现'Window_Loaded','MyBack_Click','MyWebBrowser_LoadCompleted','MyGo_Click'和'MyForward_Click'方法。有关示例,请参见[WPF WebBrowserControl](http://www.dotnetperls.com/webbrowser-wpf)。 – Tim

+0

蒂姆你正确的感谢。 –

回答

0

虽然实现在代码中“点击”事件背后将使该代码的工作,你真的需要看看MVVM ....

http://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute

就拿我对于它来说,任何超过几行并且不使用MVVM的项目都将是不可维护的意大利面条代码....我已经看到(并且可悲地不得不努力工作)这样的项目,所以请做,正确,因为有一天我可能必须自己处理你的代码........

相关问题