Mono.android Foundation

Source: Internet
Author: User

Mono.android Base (Address)

The MONO.ANDROID project structure is
— Project + Assets + Resources     + drawable     + layout     + values      Resource.Designer.cs  XXActivity.cs  

Where the Layout folder holds the app's front-end UI file, the front-end UI is an XML file with a suffix of. Axml, which has two views:Design and Source. Supports drag-and-drop of visual controls in the Design view. The backend of the app is the activity class, and the classes you write inherit the base class activity and manipulate the front-end page controls in your own class. The Assets folder holds the project's static files, such as your outline XML file, where the files can be read by the Assets.Open() following stream methods:

        using (StreamReader sr = new StreamReader(Assets.Open("sample.xml")))        {            string content = sr.ReadToEnd();        }

The Resource.Designer.cs file records the IDs of the controls in all projects and also includes the UI page. Sometimes when a new control is added to the page, its ID is not automatically added to the Resource.Designer.cs file, or the file is not regenerated. In this case, you can click the Save All button, then click the Refresh icon in the Solution window, then open file Resource.Designer.cs , and then close the file Resource.Designer.cs. If it still doesn't work, you can check the project file (xx.csproj, open with Notepad) to make sure the following three lines are present:

<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix><AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile><AndroidResgenClass>Resource</AndroidResgenClass>
Front-End UI page associated with activity

Use SetContentView(Resource.Layout.Main) the activity class to associate to the front-end page. After the association is complete, you can FindViewById() obtain the controls defined in the page.

        // Set our view from the "main" layout resource        SetContentView(Resource.Layout.Main);        // Get our button from the layout resource,        // and attach an event to it        Button button = FindViewById<Button>(Resource.Id.MyButton);                  button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };

The activity's MainLauncher=true identity, which identifies the file as the application's entry.

The initial code is as follows:

Using android.app;using android.widget;using android.os;using system.io;using system.xml;namespace example.mono.android{[Activity (Label = "Example.Mono.Android", Mainlauncher = true, Icon = "@drawable/icon")] Publi        C class Mainactivity:activity {int count = 1; protected override void OnCreate (bundle bundle) {base.            OnCreate (bundle);            Set our view from the "main" layout resource Setcontentview (Resource.Layout.Main); Get our buttons from the layout resource,//and attach a event to it button button = findviewbyi                      D<button> (Resource.Id.MyButton); button. Click + = delegate {button. Text = string. Format ("{0} clicks!", count++);            }; using (StreamReader sr = new StreamReader (Assets.open ("Sample.xml"))) {string content = Sr.                ReadToEnd ();                XmlDocument XDoc = new XmlDocument ();      Xdoc.loadxml (content);          var level = Xdoc.selectnodes ("//secondlevel[@id = ' SL1 ']"); }        }    }}
About page Jumps

Add the new activity class to the project by adding the page to the layout Second.axml SecondActivity.cs . On the main page, click button, and then jump to the second page and pass the parameters past.  The creation of a new instance of the activity is used Intent to intent the current activity in the context of the operation, using the SecondActivity type initialization intent, that is var secondActivity = new Intent(this, typeof(SecondActivity)); . Use secondActivity.PutExtra() to upload parameters to the second page secondActivity.PutExtra("Arg1", "Argument from main page!"); . Start the intent, StartActivity(secondActivity); . The code is as follows:

            button.Click += delegate {                var secondActivity = new Intent(this, typeof(SecondActivity));                secondActivity.PutExtra("Arg1", "Argument from main page!");                StartActivity(secondActivity);            };

In the OnCreate method of the second page, use Intent.GetStringExtra the parameter that accepts the pass. The code is as follows:

[Activity(Label = "SecondActivity")]public class SecondActivity : Activity{    protected override void OnCreate(Bundle bundle)    {        base.OnCreate(bundle);        // Create your application here        SetContentView(Resource.Layout.Second);        TextView textView1 = FindViewById<TextView>(Resource.Id.textView1);        var argument = Intent.GetStringExtra("Arg1") ?? "Not Available";        textView1.Text = "Welcome! It‘s TextView from second page." + argument;    }}

Mono.android Foundation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.