wpf 错误 - 已经使用相同的参数类型定义了一个名为“InitializeComponent”的成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25428175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:23:38  来源:igfitidea点击:

Error - already defines a member called 'InitializeComponent' with the same parameter types

c#wpfwindowinitializationmultiple-definition-error

提问by Diaconescu Cristian

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"

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

回答by Eugene Podskal

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

Window在 Visual Studio 中创建的WPF类通常具有InitializeComponent用于初始化其属性和内容的方法 - InitializeComponent() 做什么,它在 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.cstemporary file with initialization code will be opened.

它是从您的 XAML 标记生成的,不包含在您的代码隐藏 .cs 文件中,但对于编译器(和 msbuild.exe)来说,它仍然是 Window 类的有效内部部分 - 如果您创建新的空 Window 并单击在InitializeComponent()调用时*.g.i.cs,将打开一个带有初始化代码的临时文件。

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

因此,当您将另一个InitializeComponent方法放入代码隐藏文件时,它会导致方法定义不明确。



SOLUTION:

解决方案:

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

将您的自定义方法重命名为InitializeComponentsCustom并在构造函数中调用它:

public MainWindow()
{
    InitializeComponent();

    InitializeComponentsCustom();
}

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

orjust put the entire code from book method into the constructor(just do not remove the original InitializeComponentcall).

或者只是将 book 方法中的整个代码放入构造函数中(只是不要删除原始InitializeComponent调用)。

回答by CodeCaster

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 anda XAML template for your window, complete with an automatically generated InitializeComponent() method.

要创建此示例,您必须从头开始编写 Window1 类(右键单击解决方案资源管理器并选择添加 -> 类以开始)。您不能选择“添加”->“窗口”,因为这将为您的窗口添加代码文件XAML 模板,并带有自动生成的 InitializeComponent() 方法。

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

您得到的错误还告诉您:您正在尝试定义另一种InitializeComponent()方法。

回答by Haojie

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

InitializeComponent当您定义 MainWindow.xaml 时,方法会在 MainWindow.g.cs 中自动生成。

回答by Dennis

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

对我来说,它使用的是 Cocoa Framework Binding 和 Xamarin iOS

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

我使用的视图有两个名为 init() 的构造函数,它们具有不同的重载。

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

我在 ApiDefinition.cs 中删除了其中一个,这对我有用。也许这会帮助某人。