Home > Back-end >  Wpf: Warning CS0108: 'MainWindow.Close' hides inherited member 'Window.Close()'.
Wpf: Warning CS0108: 'MainWindow.Close' hides inherited member 'Window.Close()'.

Time:12-04

I have Wpf 7.0 Error

CodePudding user response:

You have given the MenuItem the name "Close". When assigning a name to an element, Designer Studio's code generator creates a field with that name.

You can see the file generated by the code generator if you move the cursor to "InitializeComponent" and press F12. On line 47 (this is the number I have, you may have a shift, but not much) you will see "internal System.Windows.Controls.MenuItem Close;".

That is, in fact, you are trying to create a field with the same name as the "Close ()" method already present in the base type. The Studio warns you about this overlap.

To fix it, change the name of the element:

    <MenuItem x:Name="miClose" FontSize="20" Header="_Close" Click="Close_OnClick"/>

Keep in mind that the warning may not disappear immediately. The studio does not always correctly track changes made by the code generator.
But when you re-open the Solution, this warning will definitely be reset.

  •  Tags:  
  • wpf
  • Related