The type "MyApp" already contains a definition for "MystatusBar",

MyApp.XAML

<StatusBar Name="MystatusBar" DockPanel.Dock="Bottom" BorderBrush="Black"
                 Background="{StaticResource DarkBrush}">
                <StatusBarItem>
                    <TextBlock x:Name="m_StatusBarLineInfo"/>
                </StatusBarItem>
                <StatusBarItem>
                    <Separator/>
                </StatusBarItem>
                <StatusBarItem>
                    <TextBlock x:Name="m_StatusBarMessage"/>
                </StatusBarItem>
            </StatusBar>

      

MyApp.g.cs

     #line 722 "..\..\MyApp.xaml"
        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
        internal System.Windows.Controls.Primitives.StatusBar MystatusBar;

 case 106:
            this.MystatusBar = ((System.Windows.Controls.Primitives.StatusBar)(target));
            return;

      

MyApp.xaml.cs

internal StatusBar MystatusBar;
case 110:
                    this.MystatusBar = (StatusBar)target;
                    break;

      

This is a custom StatusBar element. The error says: "The type MyApp already contains a definition for" MystatusBar "

The MyApp.g.cs file is generated automatically when the project is created. I am stuck on this.

+3


source to share


2 answers


Whenever you name a XAML element, how do you do it:

<StatusBar Name="MystatusBar" ... />

      

Visual studio will create a member for you with that name so that you can access it from code. This is the instance you see in MyApp.g.cs , which is the generated file. It is generated automatically when the XAML changes.



Therefore, when you try to declare another reference with the same name in your code behind ( MyApp.xaml.cs ), you will correctly get the error, since all three of these files are compiled into the same class.

You do not need to announce your leadership. You can only use the one that Visual Studio has already created for you.

+1


source


Whenever you get an error that says something like the following, it is usually correct:

The type "MyApp" already contains a definition for "MystatusBar"

This usually means that there is already a named member MyApp

declared in your class MystatusBar

. It may be a different type of penis or even enum

, but you will have one there.



A good way to find this is to go to the bug declaration (double member declaration) and comment it out. The error always occurs in the second ad, or in the ad further down the page. Copy the duplicated element and paste it at the top of the file, just inside the class definition.

On recompilation, the error should now display the declaration of the other member, so you know where it is after that.

If that doesn't work and Visual Studio gets confused, just save your work and then close it and reopen it. After that it should be good.

+1


source







All Articles