This post contains all the code that’s been written in this YouTube video.
Main.axml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="0" android:textSize="50sp" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/txtNumber" android:layout_marginBottom="20dp" android:layout_marginTop="20dp" /> <Button android:text="Increment" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnIncrement" /> <Button android:text="Decrement" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnDecrement" /> </LinearLayout>
MainActivity.cs
using Android.App; using Android.Widget; using Android.OS; namespace XamarinTut { [Activity(Label = "XamarinTut", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { TextView txtNumber; int number; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView (Resource.Layout.Main); txtNumber = FindViewById<TextView>(Resource.Id.txtNumber); FindViewById<Button>(Resource.Id.btnIncrement).Click += (o, e) => txtNumber.Text = (++number).ToString(); FindViewById<Button>(Resource.Id.btnDecrement).Click += (o, e) => txtNumber.Text = (--number).ToString(); } } }
Hi,
nice tutorial. Very well explained.
I got stuck with some error messages though.
At the end I have copied your original source code for both files into my project. (Did a restart on VS as well)
But I still get the following error messages:
‘Resources.Layout’ does nor contain a definition for ‘Main’
‘Resources.Id’ does not contain a definition for ‘txtNumber’
‘Resources.Id’ does not contain a definition for ‘btnIncrement’
‘Resources.Id’ does not contain a definition for ‘btnDecrement’
Could you gice some advice what went wrong?
One issue I’ve had is that for no obvious reason, I get
Severity Code Description Project File Line Suppression State
Error CS0103 The name ‘Resource’ does not exist in the current context KeyAttemp3 P:\C#\Android\KeyAttemp3\KeyAttemp3\MainActivity.cs 21 Active
SetContentView(Resource.Layout.activity_main);
Why is the section called Resources but it is accessed via Resource?
Hi
Pokemon
I agree
Hi, I am a new coder, build this code but error happened, its shown “CS0117 ‘Resource’ not include ‘id’ define…”
I check tying again n again (android:id=”@+id/txtNumber”), clean/re-build too, but still experience this error, can you let me know whats wrong?
The Code worked for me, Thanks!!