MainActivity.cs 中未识别 ID
Id not being identified in MainActivity.cs
我正在使用 Visual Studio Xamarin 开发一个 Android 应用程序,使用 AXML 和 C#。我以前在另一个项目中制作过这个应用程序,但不小心,我重命名了一些文件,然后它就出了问题。所以我决定制作一个新的解决方案并复制其中的代码。我只是按 Ctrl+C、Ctrl+V 编辑了 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 does not contain a definition for "The ID I used for the control"
Please help.
通过关闭 Visual Studio 并重新启动我的电脑解决了问题。
我正在使用 Visual Studio Xamarin 开发一个 Android 应用程序,使用 AXML 和 C#。我以前在另一个项目中制作过这个应用程序,但不小心,我重命名了一些文件,然后它就出了问题。所以我决定制作一个新的解决方案并复制其中的代码。我只是按 Ctrl+C、Ctrl+V 编辑了 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 does not contain a definition for "The ID I used for the control" Please help.
通过关闭 Visual Studio 并重新启动我的电脑解决了问题。