[英]C# web browser constructor
我正在嘗試將Web瀏覽器添加到現有的C#應用程序中,但是,在大約6年內沒有使用過C#,我對它的工作原理並不熟悉。
我正在嘗試使用以下代碼將瀏覽器添加到partial class
(再次,我不熟悉的東西):
public partial class WebBrowser : WebBrowserBase{
public WebBrowser(){
...
}
...
}
但是,我在構造函數上遇到編譯錯誤:
'WebBrowserBase'不包含帶0參數的構造函數
我谷歌了,並在SO上遇到了以下問題: C#錯誤:父不包含帶0參數的構造函數 。 我嘗試做了回答中的建議,並將我的代碼更改為:
public partial class WebBrowser : WebBrowserBase{
public WebBrowser(int i) : base(i){
...
}
...
}
但是,我得到一個編譯錯誤,說:
'WebBrowserBase'不包含帶有1個參數的構造函數
所以我猜這個問題與構造函數中的參數數量無關......有誰可以解釋我在這里做錯了什么?
如果您查看WebBrowserBase類,它會聲明:
“此API支持產品基礎結構,不能直接在您的代碼中使用。”
似乎它沒有任何公共構造函數 - 所以你不能繼承它。 但是如果你不想創建自己的WebBrowser
控件(改變它的一些功能),你應該在XAML
視圖中使用默認的System.Windows.Forms.WebBrowser
:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="525"
Height="350">
<WebBrowser HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Window>
In Inheritance,
If Derived class contains its own constructor which not defined in Base class then this error Occurs
For Example:
class FirstClass
{
public FirstClass(string s) { Console.WriteLine(s); }
}
class SecondClass : FirstClass
{
public SecondClass()
{
Console.WriteLine("second class");
}
}
Output: Error:-'myconsole.FirstClass' does not contain a constructor that takes 0 arguments
To Run without Error:
class FirstClass
{
public FirstClass()
{
Console.WriteLine("second class");
}
public FirstClass(string s) { Console.WriteLine(s); }
}
class SecondClass : FirstClass
{
public SecondClass()
{
Console.WriteLine("second class");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.