wallah wallah - 5 months ago 29
C# Question

using method in namespace that have same name with I'm working now on

It is strange....

there is a namespace named Android.Graphics and I'm work in the namespace named Xamarin.FormsBook.Platform.Android.

There is one more namespace named Xamarin.Forms and it has Color field.

Now, I want to use Android.Graphics.Color so I write Color then something recognize it as Xamarin.Forms.Color not Android.Graphics.Color.

enter image description here

And, If I write Android.Graphics.Color the builder could not find Android.Graphics. If I write Android. then builder find Xamarin.FormsBook.Platform.Android 's methods and fields.

enter image description here

If I write Android.Graphics.Color outside of namespace region, then it recognize.

enter image description here

But I can't assign any value in it since 'A namespace can not directly contain members such as fields or methods'.

help me, I want to use Color of Android.Graphic


You can do this by using an alias directive:

using AndroidGraphics = Android.Graphics;

You can then refer to the Android.Graphics.Color type by using the alias name AndroidGraphics.Color. Since there isn't another namespace called AndroidGraphics your compiler will resolve it correctly.

Alias directives can also be used for individual types, so you could also do the following:

using AndroidColor = Android.Graphics.Color;

and then refer to the type using AndroidColor.