1

I am trying to add a web browser to an existing C# application, but, having not used C# in about 6 years, I am quite unfamiliar with how it works.

I am trying to add the browser to a partial class (again, something I am not familiar with) using the following code:

public partial class WebBrowser : WebBrowserBase{
    public WebBrowser(){
        ...
    }
    ...
}

However, I am getting a compile error on the constructor that says:

'WebBrowserBase' does not contain a constructor that takes 0 arguments

I Google'd this, and came across the following question on SO: C# Error: Parent does not contain a constructor that takes 0 arguments. I tried doing what was suggested in the answer to this, and changed my code to:

public partial class WebBrowser : WebBrowserBase{
    public WebBrowser(int i) : base(i){
        ...
    }
    ...
}

However, I then get a compile error that says:

'WebBrowserBase' does not contain a constructor that takes 1 arguments

So I'm guessing that this issue isn't to do with the number of arguments in the constructor... Can anyone explain what I'm doing wrong here?

Community
  • 1
  • 1
Noble-Surfer
  • 3,052
  • 11
  • 73
  • 118
  • What are you going to use it for? Depending on this I would suggest the use of XAML or a normal webrequest – Tom Droste Apr 28 '16 at 11:53
  • Basically, I want to display the current application as it is, inside the web browser, giving the user the option to switch between the application display and a web page display as the application will, in various places, give the user the option to open up a particular web page. I have never used XAML- this is the first time I have come across it, and am not really sure how to use it/ integrate it with code that I will write manually myself... – Noble-Surfer Apr 28 '16 at 12:15
  • And what technology stack is the main app done in? As you haven't used XAML? Or are we talking about some proof of concept? – Petr Vávro Apr 28 '16 at 12:21
  • I have only just started working on this- I think the GUI as it currently stands has been created using XAML (there are .xaml.cs files in the solution)- the main application is written in C#. So I guess it is best to use XAML to edit the GUI, rather than writing in my own C#? I've never used XAML before, and have generally just written the source for any GUIs I've created myself (mainly Java applications, using the `swing` framework). In that case, I'll Google a XAML tutorial. – Noble-Surfer Apr 28 '16 at 12:34

2 Answers2

2

If you have a look at WebBrowserBase Class it states that:

"This API supports the product infrastructure and is not intended to be used directly from your code."

And it seems that it doesn't have any public constructor - so you can't inherit from it. But if you don't want to create your own WebBrowser control (alter some of it's functionality), you should just use the default System.Windows.Forms.WebBrowser in a XAML View:

<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>
Petr Vávro
  • 1,506
  • 1
  • 10
  • 12
1
  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");
    }
}
King
  • 179
  • 5