The type or namespace 'App' does not exist

2019-02-12 18:37发布

问题:

I am having an issue with a newly made Xamarin.Forms app in Visual Studio 2015. I added the Droid/iOS projects to the solution and it is giving me a build error saying...

The type or namespace 'App' does not exist in the current namespace

Here is an example of where the two errors are.

Droid project:

namespace MyApp.Droid
{
    [Activity (Label = "MyApp", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
    {
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            global::Xamarin.Forms.Forms.Init (this, bundle);
                LoadApplication (new MyApp.App ());
                //Error on the above line at MyApp.App ()
            }
        }
}

iOS project:

namespace MyApp.iOS
{
    [Register("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
    {
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init ();
            LoadApplication (new MyApp.App ());
            //Error on above line in MyApp.App ()
            return base.FinishedLaunching (app, options);
        }
    }
}

This solution was just made and no coding done yet, could this be an issue with VS2015?

回答1:

This is an issue that still exists. While this may not have been the solution back in July, this is the working solution for 3/31/16.

  • Clean the solution
  • Build the PCL
  • Remove the PCL reference from the affected project(s)
  • Re-add the PCL reference
  • Build

This also happens sometimes after opening a XAML file and switching back to a C# file in the PCL project.



回答2:

You need to reference the Forms PCL project, it will do it by default when you create the project unless you create each project individually. Just Right Click on the MyApp.Droid project -> Add Reference -> Solution -> Check MyApp(?!?!)



回答3:

The answer of @silencedmessage helped me.

Though I would like to add that I noticed that all my Xamarin projects did not even have a PCL reference to start with. I started with a blank Xamarin Forms XAML project template for all possible targets and I did not even bother checking the reference to the PCL, before I read this answer.

So long story short: Even from a blank official template you should still check that the PCL reference is added to all projects. Weird stuff.