CS0117 - Xamarin 未检测到“资源"文件夹和文件 [英] CS0117 - Xamarin not detecting 'Resources' folders and files

查看:26
本文介绍了CS0117 - Xamarin 未检测到“资源"文件夹和文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发 Xamarin.Forms 应用程序,最初在 iOS 上,现在在 Android 上.我遇到了一个问题,即我的代码无法看到我的 MainActivity.cs 中引用的资源.对于两个不同的资源,我有同样的错误.

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.

我正在学习 Udemy 课程,讲师强调手动构建一些 xml 文件,因此有两种类型,axmlxml.

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.

第一个错误

/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)

第二个错误:

/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 其中包含 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>

资源

~/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'.

当我手动将文件夹拖放到正确的位置时,问题就出现了.但似乎 Xamarin 只能识别通过右键单击选项添加的文件和文件夹,然后编译器只能看到位于不正确的原始位置的文件.

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.

但是,简单地通过右键单击/删除"删除文件并不能解决问题.虽然目标文件已从 Xamarin UI 中删除,但并未从磁盘上的项目目录中删除,因此无法添加具有相同名称的新文件.我必须导航到项目文件夹并运行 rm -rf menu 将其删除.然后返回 Xamarin 并右键单击/添加/新建文件夹和新建文件.

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.

Xamarin 论坛上的这篇帖子让我了解到问题所在.

This post on Xamarin forums tipped me off to what the issue was.

Xamarin 帖子

这篇关于CS0117 - Xamarin 未检测到“资源"文件夹和文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆