在我正在使用的名称空间中使用具有相同名称的方法

时间:2016-11-18 05:57:11

标签: c# namespaces

很奇怪......

有一个名为 Android.Graphics 的命名空间,我在名为 Xamarin.FormsBook.Platform.Android 的命名空间中工作。

还有一个名为 Xamarin.Forms 的命名空间,它有Color字段。

现在,我想使用 Android.Graphics.Color ,所以我写了Color然后把它识别为 Xamarin.Forms.Color 而不是 Android.Graphics。颜色

enter image description here

而且,如果我写 Android.Graphics.Color ,则构建器找不到Android.Graphics。如果我写Android。然后构建器找到 Xamarin.FormsBook.Platform.Android 的方法和字段。

enter image description here

如果我在命名空间区域之外写 Android.Graphics.Color ,那么它就会识别。

enter image description here

但我无法在其中分配任何值,因为'命名空间不能直接包含字段或方法等成员'。

帮助我,我想使用Color of Android.Graphic

1 个答案:

答案 0 :(得分:1)

您可以使用alias directive

来完成此操作
using AndroidGraphics = Android.Graphics;

然后,您可以使用别名Android.Graphics.Color来引用AndroidGraphics.Color类型。由于没有另一个名为AndroidGraphics的命名空间,您的编译器将正确解析它。

Alias指令也可用于各种类型,因此您还可以执行以下操作:

using AndroidColor = Android.Graphics.Color;

然后使用AndroidColor引用类型。