2

I tried to make an example of the book that shows exactly

private Button button1;
public MainWindow()
{
    InitializeComponent();
}
private void InitializeComponent()
{
    // Configure the form.
    this.Width = this.Height = 285;
    this.Left = this.Top = 100;
    this.Title = "Code-Only Window";
    // Create a container to hold a button.
    DockPanel panel = new DockPanel();
    // Create the button.
    button1 = new Button();
    button1.Content = "Please click me.";
    button1.Margin = new Thickness(30);
    // Attach the event handler.
    button1.Click += button1_Click;
    // Place the button in the panel.
    IAddChild container = panel;
    container.AddChild(button1);
    // Place the panel in the form.
    container = this;
    container.AddChild(panel);
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    button1.Content = "Thank you.";
}

But it gives me an error:

"Type 'WpfApplication1.MainWindow' already defines a member called 'InitializeComponent' with the same parameter types"

Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
  • 4
    My guess is you've copied `InitializeComponent` from some other source into the class when that method already exists (either in the same file or in another file if it uses partial classes). – D Stanley Aug 21 '14 at 13:55
  • 3
    **Read** the error. Comment the `InitializeComponent()` method you typed and right-click the `InitializeComponent()` in your constructor and click "Go To Definition", you'll see where it is already defined. – CodeCaster Aug 21 '14 at 13:56
  • http://stackoverflow.com/a/5021568/142904 – Kristian Damian Aug 21 '14 at 13:58
  • 1
    @HighCore there's a note in said book ("Pro WPF: Windows Presentation Foundation", chapter 2): _"Code-only development is a less common [...] avenue for writing a WPF application without any XAML"_. – CodeCaster Aug 21 '14 at 13:58

4 Answers4

4

WPF Window class created in Visual Studio usually has InitializeComponent method that is used to initialize its properties and contents - What does InitializeComponent() do, and how does it work in WPF?.

It is generated from your XAML markup and is not contained in your code-behind .cs file, but for the compiler(and msbuild.exe) it is still a valid intrinsic part of the Window class - if you create new empty Window and click on InitializeComponent() call then a *.g.i.cs temporary file with initialization code will be opened.

So, when you put another InitializeComponent method into the code behind file it causes ambiguous method definition.


SOLUTION:

Either rename your custom method to InitializeComponentsCustom and call it in the constructor:

public MainWindow()
{
    InitializeComponent();

    InitializeComponentsCustom();
}

private void InitializeComponentsCustom()
{
    // ...
}

or just put the entire code from book method into the constructor(just do not remove the original InitializeComponent call).

Community
  • 1
  • 1
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
  • This is very informative. I had a lot of "memeber already defined" errors because i decompiled an old application and it had the same issues every where. I commented out these conflicts and application worked perfectly. Thanks again – MindRoasterMir Jan 12 '19 at 10:51
1

InitializeComponent method is generated automatically in MainWindow.g.cs when you define MainWindow.xaml.

Haojie
  • 5,665
  • 1
  • 15
  • 14
0

Like I said in my comment, read. Reading properly is a virtue you'll need during your programming career.

You need to closely follow the note in the book:

To create this example, you must code the Window1 class from scratch (right-click the Solution Explorer and choose Add -> Class to get started). You can't choose Add -> Window, because that will add a code file and a XAML template for your window, complete with an automatically generated InitializeComponent() method.

The error you get also tells you this: you're trying to define another InitializeComponent() method.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • 10
    Your patronising tone is unhelpful. No need for slapped wrists, just state the answer or say nothing. – LordWilmore Aug 10 '15 at 11:49
  • @LordWilmore it wasn't meant patronizing or condescending, but I see how it could be interpreted like that. Thanks for your explanation. Still, when you're following a tutuorial but only read half of it, you're bound to run into problems: that's what I try to address in the first line of my answer. – CodeCaster Aug 10 '15 at 11:52
  • It is very good suggestion, nothing wrong if I think positively thanks – MindRoasterMir Jan 12 '19 at 10:53
0

For me it was using a Cocoa Framework Binding with Xamarin iOS

The View i used had two constructors named init() with different overloads.

I deleted one of them in the ApiDefinition.cs, which did the trick for me. Maybe this will help someone.

Dennis
  • 1