65

In a simple WPF, I have renamed MainWindow class to MyClass and I got this error in App.g.cs:

    public static void Main() {
        MyClass.App app = new MyClass.App();
        app.InitializeComponent();
        app.Run();
    }

How can I fix this can't find anything on the internet ?

2
  • 1
    Is App a nested class inside MyClass?
    – decyclone
    Dec 18, 2010 at 20:52
  • 1
    Or maybe MyClass is a namespace ? MyClass.App app looks weird to me.
    – TarasB
    Dec 18, 2010 at 20:56

6 Answers 6

190

I accidentally got this error, in my case I had a class with the same name as the namespace. Renaming the class solved the problem.

5
  • Thanks, Johan, I had this problem too just now. What a fantastically stupid reason to give an error. Mar 30, 2012 at 19:04
  • 2
    Thanks, I just wasted about an hour.
    – Golvellius
    Sep 20, 2013 at 9:26
  • 1
    For more info: blogs.msdn.microsoft.com/ericlippert/2010/03/09/… Mar 23, 2016 at 9:56
  • Seems so unlikely yet so common error! Same was the case with me.
    – zar
    May 14, 2020 at 20:15
  • I just ran into this because I named a usercontrol the same name as my project
    – bwoogie
    Jan 11, 2021 at 19:22
17

Do not name a class the same as its namespace

Check out if you don't have a class somewhere in your project that has the same name as your namespace!

for example:

namespace HashCalculator
{
  public class HashCalculator
4

App.g.cs is produced by compiling App.xaml into C# code. The problem is in your App.xaml. Without seeing it, I can't be sure exactly what, but you probably just need to alter the StartupUri to StartupUri="MyClass.xaml".

4

You forgot to change the classname in MyClass.xaml (or MainWindow.xaml).

change it from

<Window x:Class="YourNamespace.MainWindow"

to

<Window x:Class="YourNamespace.MyClass"
0

Johan's answer + Also check if you have a folder with the same name as your solution.

0

In my case I needed to install the .NET Framework 4.8 Runtime and Targeting Pack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.