Reputation: 1957
I have a Mono for Android project that compiles and runs successfully with the following code. However a copy/paste into a newer project I'm working on results in a compiler error indicating: [appnamespace].Android.Resource.Layout does not contain a definition for 'SimpleListItem2'.
I'd expect that error if I was trying to access an xml resource layout I defined, but I'm trying to access the default ones provided by Google, specifically 'SimpleListItem2'. Is there something I need to do get the compiler to recognize the default layouts? Thanks!
public override View GetView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null) {
LayoutInflater li = (LayoutInflater)this.Context.GetSystemService(Context.LayoutInflaterService);
v = li.Inflate(Android.Resource.Layout.SimpleListItem2, null);
}
TextView tt = (TextView)v.FindViewById(Android.Resource.Id.Text1);
if (tt != null) { tt.Text = string.Format("{0}, {1}", this.LastName, this.FirstName); }
return v;
}
Basically, I'm looking for access to these layouts in the Mono.Android assembly, under the Android.Resource.Layout namespace:
ActivityListItem
BrowserLInkContextHeader
ExpandableListContent
PreferenceCategory
SelectDialogItem
SelectDialogMultiChoice
SelectDialogSingleChoice
SimpleDropDownItem1Line
SimpleExpandableListItem1
SimpleExpandableListItem2
SimpleGalleryItem
SimpleListItem1
SimpleListItem2
SimpleListItemChecked
SimpleListItemMultipleChoice
SimpleListItemSingleChoice
SimpleListItemDropDownItem
SimpleSpinnerItem
TestListItem
TwoLineListItem
Upvotes: 1
Views: 2952
Reputation: 13600
This is a C# language feature, and is behaving as per the C# language specification.
Consider this example:
using System;
namespace Example {
namespace System {
}
class Bad : System.Object {
}
}
The above fails to compile:
ns.cs(8,24): error CS0234: The type or namespace name `Object' does not exist in
the namespace `Example.System'. Are you missing an assembly reference?
This is true for both .NET CSC and Mono's mcs compilers.
Why? See §10.8 Namespace and type names of the C# Language Specification, pages 100-102.
- Otherwise, the namespace-or-type-name is of the form
N.I
or of the formN.I<A1, ..., AK>
.N
is first resolved as a namespace-or-type-name.
In this case we're processing System.Object
, which is of the form N.I
. So
we need to first resolve System
:
- Otherwise, if the namespace-or-type-name is of the form
I
or of the formI<A1, ..., AK>
:
...
- Otherwise, for each namespace N, starting with the namespace in which the namespace-or-type-name occurs, continuing with each enclosing namespace (if any), and ending with the global namespace, the following steps are evaluated until an entity is located: ...
This resolves the token System
to Example.System
. Now that System
is
resolved the compiler attempts to resolve System.Object
, i.e. the fully
qualified name of Example.System.Object
. This type does not exist, and we get
an error.
The fix for the above sample? Use global::
:
class Bad : global::System.Object {
}
The same is true for your Android code; if you happen to be within an
Example.Android
namespace and you need to use the Android.Resource.Layout
type, then use global::Android.Resource.Layout
.
Or you can use a using-alias, which is resolved at the point of declaration, allowing:
using System;
using MyObject = System.Object;
namespace Example {
namespace System {
}
class Bad : MyObject {
}
}
Upvotes: 9
Reputation: 1957
It appears that namespace collisions causes this error. Be warned when using "Android" in your namespace.
If the app's namespace includes "Android", Xamarin's Mono for Android compiler seems to struggle resolving Google's SimpleListItem2 object.
To experience the error, the "Android" string needs to be a complete string (not substring) in the namespace. The namespace "BenHorgen.Android.MyApps.TestApp" will cause the compiler error.
More specifically, including the string "Android" as substring in the namespace will not cause the issue. For example: "BenHorgen.MyAndroidApps.TestApp" does not cause the problem for me.
Upvotes: 7