![](/img/trans.png)
[英]Xamarin C# Resource.id does not contain a definition for 'Toolbar'
[英]xamarin C#: 'Resource' does not contain a definition for 'Id'
我最近开始使用 Xamarin 使用 C# 构建 Android 应用程序。 我遇到的一个问题让我很难取得任何进展:错误 CS0117
我有两个相同的项目,问题只出现在其中一个中。 它最初发生在两者中,但重建几次修复了第一个。 第二个似乎更持久。 我真的需要找到解决这个问题的方法,因为引用是非常基本的和需要的。 更不用说,它发生在每个新项目中。 这是我的代码: NoteMath
using System;
using Android.App;
using Android.Content;
using Android.Widget;
using Android.OS;
namespace NoteMath_v1._0._1
{
[Activity(Label = "noteMath", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Window.RequestFeature(Android.Views.WindowFeatures.NoTitle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
EditText etInput = FindViewById<EditText>(Resource.Id.etInput);
TextView tvConsole = FindViewById<TextView>(Resource.Id.tvConsole);
etInput.KeyPress += (object sender, EditText.KeyEventArgs e) =>
{
e.Handled = false;
if (e.Event.Action == Android.Views.KeyEventActions.Down && e.KeyCode == Android.Views.Keycode.Enter)
{
tvConsole.Append("\n>" + HandleInput.TransferInput(etInput.Text));
etInput.Text = "";
e.Handled = true;
}
};
}
}
}
您发布的代码看起来不错。 错误显然不是来自那里。 您的任何资源布局或 xml 文件都有问题。
问题实际上是 Visual Studio 不会在“错误”面板中显示资源生成错误,即使它应该是这样的(我认为这将在未来版本中修复)。 目前它只是告诉您该类Id
不存在。
Resources.designer.cs
是 Xamarin 在编辑任何资源文件时生成的类。 它包含对 xml/axml 文件中任何声明、id 等的引用。 如果资源有错误, Resources.designer.cs
生成将失败,但您会得到一个非常模糊的错误提示。
您可以尝试的第一件事是重建您的 Android 项目,强制再次生成所有资源。 否则请尝试以下操作:
好消息是,您可以通过将构建输出详细程度更改为“详细”来查看生成错误:
现在再次编译您的项目并检查输出窗口。 它会告诉你到底是什么问题。
修复后,将生成Resource.Id
类。
对我来说,这有效:
步骤 1. 项目 > 更新 NugetPackages
步骤 2. 构建 > 全部清理
步骤 3. 构建 > 全部构建
我是超级新人。 唯一真正对我有用的是在我的项目根目录中手动运行这个 powershell 脚本:
Get-ChildItem .\\ -include bin,obj -Recurse | foreach ($_) { remove-item $_.fullname -Force -Recurse }
我不确定为什么实时视图/构建输出没有更大的帮助,或者 Visual Studio 没有在我的 xamarin 项目中正确清理这些文件。
特别感谢这位网友:
只需更新Nuget软件包即可为我解决问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.