我正在使用Visual Studio Xamarin通过AXML和C#开发Android应用。我以前曾在另一个项目中制作过该应用程序,但无意中,我重命名了一些文件,结果一团糟。因此,我决定制作一个新的解决方案,并在其中复制代码。我只是按Ctrl + C,按Ctrl + V'ed了AXML代码,然后小心地逐行复制了C#代码。现在的问题是,C#代码无法通过其ID获取按钮组件。我的AXML中有2个EditText,1个TextView和5个按钮,每个按钮都有不同的ID。这是AXML代码的一部分-
<TextView
android:text="Result"
android:textSize="36sp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/outputText"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp" />
<Button
android:text="ADD"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnAdd"
android:onClick="addclicked"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<Button
android:text="SUBTRACT"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnSub"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
这是C#代码的一部分
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace SimpleCalculator.Droid
{
[Activity(Label = "SimpleCalculator", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public TextView outputText;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
SetContentView(Resource.Layout.Main);
Button btnAdd = FindViewById<Button>(Resource.Id.btnAdd);
Button btnSub = FindViewById<Button>(Resource.Id.btnSub);
Button btnMul = FindViewById<Button>(Resource.Id.btnMul);
Button btnDiv = FindViewById<Button>(Resource.Id.btnDiv);
Button btnClr = FindViewById<Button>(Resource.Id.btnClr);
我在使用命令FindViewById的每一行中都出现错误。错误是-
Resource.Id不包含“我用于控件的ID”的定义请帮助。
关闭Visual Studio并重新启动PC后解决的问题。