Reputation: 9454
I am developing an app using Xamarin.Forms. I find that I am unable to get the following to compile:
namespace Loopback.Sdk.Xamarin.Example.Forms.Android
{
[Activity (Label = "Loopback.Sdk.Xamarin.Example.Forms.Android.Android", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : AndroidActivity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
Xamarin.Forms.Forms.Init (this, bundle);
SetPage (App.GetMainPage ());
}
}
}
The build error is:
Loopback.Sdk.Xamarin.Example.Forms/Android/MainActivity.cs(4,4): Error CS0234: The type or namespace name `Forms' does not exist in the namespace `Loopback.Sdk.Xamarin'. Are you missing an assembly reference? (CS0234) (Loopback.Sdk.Xamarin.Example.Forms.Android)
Omitting Xamarin
from the namespace declaration solves the issue, so I can only assume I am running into some kind of namespace clash. My question is why this happens, since the namespaces seemingly do not even have the same root?
UPDATE:
I should stress the part of the exception which nails down the error:
The type or namespace name `Forms' does not exist in the namespace `Loopback.Sdk.Xamarin'.
So to perhaps rephrase the question a bit, why Loopback.Sdk.Xamarin
taken as the root namespace for Forms
, rather than just Xamarin
? How can I work around this?
Upvotes: 0
Views: 1704
Reputation: 70652
This is just how the compiler resolves namespaces. Given the presence of the namespace Loopback.Sdk.Xamarin.Example.Forms.Android
, that implies all of the namespaces within that dotted syntax. This combined with the fact that C# allows you to use partially-qualified type names (i.e. a type name with part of but not all of the namespace) means that the compiler, having matched the Xamarin
part of the namespace, assumes that Xamarin.Forms
is part of the current namespace, which includes Loopback.Sdk.Xamarin
. I.e. it assumes that the Xamarin
in Xamarin.Forms
actually refers to Loopback.Sdk.Xamarin
.
Why are you fully-qualifying the type name Xamarin.Forms.Forms
? If you have using Xamarin.Forms;
as one of your using
directives, then you should be able to just write Forms.Init()
in your code and it will work.
Alternatively, you can use a type alias. For example using Forms = Xamarin.Forms.Forms;
. That would also allow you to write Forms.Init()
in your code.
Upvotes: 0
Reputation: 4746
Its because you are using Xamarin in your namespace.
Xamarin is also a root namespace entry, hence it can't decide whether to go with your namespace hierarchy or the libraries for Xamarin.
If you must use Xamarin in your namespace, then you will have to qualify any references to the Xamarin namespace and give it an alternative name reference.
You can do this by putting something like:-
using XamarinForms = global::Xamarin.Forms;
as an example, and use this such like:-
XamarinForms.Forms.Init(this, bundle);
Upvotes: 4