【问题标题】:Xamarin Android Resource Unknown Identifier ResourceXamarin Android 资源未知标识符资源
【发布时间】:2015-10-26 12:25:04
【问题描述】:

我从 xamarin 下载了 Activity Scene Transition 示例应用程序并在 Visual Studio 2015 中打开它并尝试通过 VS Emulator 运行该应用程序。

我在 MainActivity.cs 的第 108 行收到错误“未知标识符:资源”:

view = context.LayoutInflater.Inflate(Resource.Layout.grid_item, viewGroup, false);

我没有更改任何代码,我只是将其标记为部署以在 Visual Studio 模拟器中执行应用程序。我在 VS 中时没有收到任何错误,也没有收到任何构建错误,只有在加载时我才收到此错误。

有什么我想念的吗?

【问题讨论】:

  • 检查Resources/layout中是否有grid_item.xml文件。你有没有试过彻底清理项目?
  • 浏览你的安卓资源。确保它们设置为 AndroidAsset 而不是 AndroidBundle?

标签: xamarin


【解决方案1】:

来自https://stackoverflow.com/a/36409069

在 Xamarin.Android 中存在一个长期存在的调试问题,该问题与检查静态类中的值有关。具体来说,如果您在引用静态类(或具有静态成员的非静态类)的行上设置断点,Visual Studio 可能会将检查值显示为“未知标识符:[ClassName]”。

根据我的分析,项目中类文件的位置决定了你是否会遇到这个问题。

对我来说,结果是,在 Xamarin 修复错误之前,所有静态类和具有静态成员的类都应该放在项目的根文件夹中。还有其他文件放置选项,但有些完全不起作用,并且需要使用命名空间完全限定静态类调用——即使编译器不需要。

查看下面代码中的 cmets 了解完整详情。

MainActivity.cs

using System;
using Android.App;
using Android.OS;

namespace App1 {

[Activity(Label = "Unknown Identifier Test", MainLauncher = true)]
public class MainActivity : Activity {        

    protected override void OnCreate(Bundle bundle) {
        base.OnCreate(bundle);

        Console.WriteLine(MyClass.MyString);            // Unqualified
        Console.WriteLine(App1.MyClass.MyString);       // Fully Qualified with namespace

        /*
        Set a break point on the "Console.WriteLine()" lines above and you'll get the 
        "Unknown identifier: MyClass" error when trying to inspect under specific conditions...

        File Locations                                      Unqualified             Fully Qualified
        -------------------------------------------------   ---------------------   --------------------
        MainActivity.cs in root, MyClass.cs in sub-folder   "Unknown identifier"    Inspection Works
        MainActivity.cs in sub-folder, MyClass.cs in root   Inspection Works        Inspection Works
        Both in root                                        Inspection Works        Inspection Works
        Both in different sub-folders                       "Unknown identifier"    "Unknown identifier"
        Both in same sub-folder                             "Unknown identifier"    "Unknown identifier"
        */
    }
}
}

MyClass.cs

namespace App1 {
public static class MyClass {
    public static string MyString;
}

// The class can also be constructed this way, which results in the same findings:
//public class MyClass {
//    public static string MyString;
//}    
}

2016 年 4 月 3 日,我使用此信息更新了关联的 Xamarin Bugzilla 票证。希望他们能尽快解决这个问题。

【讨论】:

    猜你喜欢
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多