Android ';MyFirstApp.Resource.Id';不包含';myButton';(CS0117)(MyFirstApp)

Android ';MyFirstApp.Resource.Id';不包含';myButton';(CS0117)(MyFirstApp),android,android-layout,xamarin,Android,Android Layout,Xamarin,主要活动我将我的项目命名为“MyFirstApp” 使用系统; 使用Android.App; 使用Android.Content; 使用Android.Runtime; 使用Android.Views; 使用Android.Widget; 使用Android.OS; 名称空间MyFirstApp { [活动(Label=“MyFirstApp”,MainLauncher=true)] 公共课活动:活动 { 整数计数=1; 创建时受保护的覆盖无效(捆绑包) { base.OnCreate(bund

主要活动我将我的项目命名为“MyFirstApp”

使用系统;
使用Android.App;
使用Android.Content;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Android.OS;
名称空间MyFirstApp
{
[活动(Label=“MyFirstApp”,MainLauncher=true)]
公共课活动:活动
{
整数计数=1;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
//从布局资源中获取我们的按钮,
//并在其上附加一个事件
Button Button=FindViewById(Resource.Id.myButton);
按钮。单击+=委派{
button.Text=string.Format(“{0}点击!”,count++);
};
}
}
}
布局分辨率/布局

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <EditText
        android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="Enter Message" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send"
        android:id="@+id/button_send" />
</LinearLayout>

字符串

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">My First App</string>
    <string name="edit_message">Enter a message</string>
    <string name="button_send">Send</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
</resources>

我的第一个应用程序
输入消息
发送
设置
主要活动
请帮助我,我是android开发的初学者 如果有什么不清楚的地方,我会尽量补充更多细节 我用Xamarin来开发

谢谢

添加属性:

android:id="@+id/myButton"
单击布局xml文件中的按钮

然后在活动中,您可以找到如下按钮:

Button button = (Button) findViewById(R.id.myButton);
而不是:

Button button = FindViewById<Button> (Resource.Id.myButton);
Button-Button=findviewbyd(Resource.Id.myButton);
更新:嗯,我推荐的findViewById是普通的Android,不确定它在Xamarin中是如何工作的,但确定您的按钮需要xml中的id


更新2:实际上你已经有了按钮的名称和Id,但它不是myButton,它是button\u send==>所以你可能想用它来代替myButton

我在你的布局中只能看到一个按钮,这个按钮的Id是button\u send。
请使用
(按钮)findviewbyd(R.id.Button\u send)进行尝试

在布局中,按钮id为-button\u send:

android:id="@+id/button_send"
所以在活动代码中,当你“找到”这个按钮时,你应该使用这个id,所以取而代之

Button button = FindViewById<Button> (Resource.Id.myButton);
Button-Button=findviewbyd(Resource.Id.myButton);
你应该使用

Button button = FindViewById<Button> (Resource.Id.button_send);
Button-Button=findviewbyd(Resource.Id.Button\u-send);

这是一个奇怪的问题,资源本身存在于与定义所有UI元素的资源不同的命名空间上


尝试使用“MyFirstApp.Android.Resource.Id.button\u send”代替。

问题是,一些内部脚手架正在生成假代码,假设添加到布局中的按钮的标识。没有上下文来确定“MyButton”支持为漫游添加的两个按钮中的哪一个。我刚刚将其重命名为“翻译按钮”,因为我首先添加了该按钮。当我开始使用真正的应用程序时,我不确定我会做什么


回到过去,我们会把这类东西交给内部用户,以确保它是合适的教学材料。

面临同样的问题

您应该执行以下操作:

1) 剪切或注释此代码片段:

Button button = FindViewById<Button>(Resource.Id.myButton);
            button.Click += delegate {
                button.Text = string.Format ("{0} clicks!", count++);
            };
Button-Button=findviewbyd(Resource.Id.myButton);
按钮。单击+=委派{
button.Text=string.Format(“{0}点击!”,count++);
};
2) 建设项目


3) 在p中添加代码。1再次(粘贴或取消注释)

哪种布局?活动\ u main.xml还是内容\ u main.xml?
Button button = FindViewById<Button>(Resource.Id.myButton);
            button.Click += delegate {
                button.Text = string.Format ("{0} clicks!", count++);
            };