Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未在MainActivity.cs中标识Id_C#_Xamarin_Visual Studio 2017 - Fatal编程技术网

C# 未在MainActivity.cs中标识Id

C# 未在MainActivity.cs中标识Id,c#,xamarin,visual-studio-2017,C#,Xamarin,Visual Studio 2017,我正在使用VisualStudioXamarin开发一个Android应用程序,使用AXML和C#。我以前在另一个项目中开发过这个应用程序,但不小心,我重命名了一些文件,结果它失控了。所以我决定制作一个新的解决方案,并复制其中的代码。我只需Ctrl+C,Ctrl+V’编辑AXML代码,然后仔细地逐行复制C代码。现在的问题是C#代码无法通过其ID获取按钮组件。我的AXML中有2个EditText、1个TextView和5个按钮,每个按钮的ID不同。下面是AXML代码的一部分- <TextVi

我正在使用VisualStudioXamarin开发一个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代码的一部分

使用系统;
使用Android.App;
使用Android.Content.PM;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Android.OS;
命名空间SimpleCalculator.Droid
{
[活动(Label=“SimpleCalculator”,Icon=“@mipmap/Icon”,Theme=“@style/MainTheme”,MainLauncher=true,ConfigurationChanges=ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
公共类MainActivity:全局::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
公共文本视图输出文本;
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
全局::Xamarin.Forms.Forms.Init(这个,savedInstanceState);
加载应用程序(新应用程序());
SetContentView(Resource.Layout.Main);
按钮btnAdd=findviewbyd(Resource.Id.btnAdd);
按钮btnSub=findviewbyd(Resource.Id.btnSub);
按钮btnMul=findviewbyd(Resource.Id.btnMul);
按钮btnDiv=findviewbyd(Resource.Id.btnDiv);
按钮btnClr=findviewbyd(Resource.Id.btnClr);
在我使用命令findviewbyd的每一行中都会出现一个错误-

Resource.Id不包含“我用于控件的Id”的定义 请帮忙


关闭Visual Studio并重新启动我的电脑解决了问题。

已解决。我不知道为什么,但当我关闭项目并重新启动电脑时,所有错误都消失了。
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);