简体   繁体   English

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

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

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"

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?在 Visual Studio 中创建的 WPF Window类通常具有用于初始化其属性和内容的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 *.gics temporary file with initialization code will be opened.它是从您的 XAML 标记生成的,不包含在您的代码隐藏 .cs 文件中,但对于 编译器(和 msbuild.exe),它仍然是 Window 类的有效内部部分 - 如果您创建新的空 Window 并单击在InitializeComponent()调用时,将打开带有初始化代码的*.gics临时文件。

So , when you put another InitializeComponent method into the code behind file it causes ambiguous method definition.因此,当您将另一个InitializeComponent方法放入代码隐藏文件时,它会导致方法定义不明确。


SOLUTION:解决方案:

Either rename your custom method to InitializeComponentsCustom and call it in the constructor:将您的自定义方法重命名为InitializeComponentsCustom并在构造函数中调用它:

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).或者只是将 book 方法中的整个代码放入构造函数中(只是不要删除原始的InitializeComponent调用)。

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

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

The error you get also tells you this: you're trying to define another InitializeComponent() method.您得到的错误还告诉您:您正在尝试定义另一个InitializeComponent()方法。

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.我在 ApiDefinition.cs 中删除了其中一个,这对我有用。 Maybe this will help someone.也许这会帮助某人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 已经使用相同的参数类型定义了一个名为“InitializeComponent”的成员 - Already defines a member called 'InitializeComponent' with the same parameter types 错误已使用相同的参数类型定义了一个名为“索引”的成员 - Error already defines a member called 'Index' with the same parameter types 类型'Startup'已经定义了一个名为'Configuration'的成员,它具有相同的参数类型 - Type 'Startup' already defines a member called 'Configuration' with the same parameter types Web Api已经使用相同的参数类型定义了一个名为“ Get”的成员 - Web Api already defines a member called 'Get' with the same parameter types 已经定义了一个使用相同参数类型调用的成员 c# - already defines a member called with the same parameter types c# “'Form1' 已经定义了一个名为 '.ctor' 的成员,具有相同的参数类型” - "'Form1' already defines a member called '.ctor' with the same parameter types " C#错误:类型'x'已经定义了一个名为'y'的成员,它具有相同的参数类型 - C# error: Type 'x' already defines a member called 'y' with the same parameter types 错误1类型“ Pay”已经使用相同的参数类型定义了一个名为“ ComputePay”的成员 - Error 1 Type 'Pay' already defines a member called 'ComputePay' with the same parameter types 我在尝试插入公共类时出错,“PlayerCollision”已经定义了一个名为“OnCollisionEnter”的成员,其参数类型相同 - I have a error trying to insert a public class , 'PlayerCollision' already defines a member called 'OnCollisionEnter' with the same parameter types 我该如何修复错误:Type Form1'已经定义了一个名为'Dispose'的成员,它具有相同的参数类型? - How can i fix the error: Type Form1' already defines a member called 'Dispose' with the same parameter types?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM