I have been developing a Xamarin.Forms
app, initially on iOS and now for Android.
I have run into a problem whereby my code is unable to see the resources being referenced in my MainActivity.cs
.
I have the same error for two different resources.
I am following along with a Udemy course and the instructor is emphasising manually building some of the xml
files, hence the two types, axml
and xml
.
First error
/Users/richardcurteis/Desktop/OneDrive/DevShared/XamarinProjects/NoteTaker/Droid/MainActivity.cs(35,35):
Error CS0117: `NoteTaker.Droid.Resource' does not contain a definition for `Menu' (CS0117) (NoteTaker.Droid)
Second error:
/Users/richardcurteis/Desktop/OneDrive/DevShared/XamarinProjects/NoteTaker/Droid/MainActivity.cs(21,21):
Error CS0117: `NoteTaker.Droid.Resource.Id' does not contain a definition for `action_add' (CS0117) (NoteTaker.Droid)
MainActivity.cs
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using SupportToolbar = Android.Support.V7.Widget.Toolbar;
using Android.Support.V7.App;
using System.Collections.Generic;
namespace NoteTaker.Droid
{
[Activity (Label = "Note Taker", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : AppCompatActivity
{
int count = 1;
private SupportToolbar toolbar;
protected override void OnCreate (Bundle savedInstanceState)
{
Xamarin.Insights.Initialize (XamarinInsights.ApiKey, this);
base.OnCreate (savedInstanceState);
new Database ();
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
toolbar = FindViewById<SupportToolbar> (Resource.Id.toolbar);
SetSupportActionBar (toolbar);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button> (Resource.Id.myButton);
button.Click += delegate {
button.Text = string.Format ("{0} clicks!", count++);
};
}
public override bool OnCreateOptionsMenu (IMenu menu)
{
MenuInflater.Inflate (Resource.Menu.addmenu, menu);
return base.OnCreateOptionsMenu (menu);
}
public override bool OnOptionsItemSelected (IMenuItem item)
{
switch (item.ItemId)
{
case Resource.Id.action_add:
Console.WriteLine ("Add button clicked");
return true;
default:
return base.OnOptionsItemSelected (item);
}
}
}
}
addmenu.xml
Which contains the action_add
id.
<?xml version="1.0" encoding="UTF-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/action_add"
android:icon="@drawable/addIcon"
android:title="Add"
app:showAsAction="always"
/>
</menu>
Resources
tree
~/Desktop/OneDrive/DevShared/XamarinProjects/NoteTaker/Droid
tree Resources/
Resources/
├── AboutResources.txt
├── Resource.designer.cs
├── drawable
│ └── addIcon.png
├── layout
│ ├── Main.axml
│ └── Toolbar.xml
├── menu
│ └── addmenu.xml
├── mipmap-hdpi
│ └── Icon.png
├── mipmap-mdpi
│ └── Icon.png
├── mipmap-xhdpi
│ └── Icon.png
├── mipmap-xxhdpi
│ └── Icon.png
├── mipmap-xxxhdpi
│ └── Icon.png
└── values
├── Strings.xml
└── styles.xml
9 directories, 13 files
Ok so I figured out what the issue was. Previously when I had been coding I had initially placed the 'menu' folder in the wrong directory. I had done this by right clicking accidentally 'layout' and clicking 'Add/New Folder'.
The problem came when I then manually dragged and dropped the folder into the correct place. But it seems that Xamarin only recognises files and folders that are added via the right click option and the compiler then only saw the files in their original, incorrect location.
However simply deleting the files with right click/'Remove' does not solve the issue.
Whilst the target file was removed from the Xamarin UI, it was not removed from the project directory on disk and as such a new file with the same name cannot be added.
I had to navigate to the project folder and run rm -rf menu
to remove it.
Then go back to Xamarin and right click/Add/New Folder and New File.
Following that I cleaned the project and rebuilt and it worked like a charm.
This post on Xamarin forums tipped me off to what the issue was.