Mono.Android 基础

Mono.Android 基础 (地址

Mono.Android项目结构是

— Project
 + Assets
 + Resources
     + drawable
     + layout
     + values
      Resource.Designer.cs
  XXActivity.cs

其中, Layout文件夹下存放App的前端UI文件,前端UI是一个后缀名为.axml的XML文件,该文件有两个视图:DesignSource。在Design视图中支持可视化控件的拖拽。 App的后端是Activity的类,自己写的类都要继承基类Activity, 并在自己类中操作前端页面的控件。 Assets文件夹下存放项目的静态文件,例如你的大纲XML文件等,这里的文件可以通过以下流方法Assets.Open()读取:

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

Resource.Designer.cs文件会记录所有项目中的控件的Id, 也包括UI页面。有时候在页面上加入一个新的控件以后,它的Id并没有自动加入Resource.Designer.cs这个文件,或者是这个文件没有重新生成。出现这个情况,一是可以单击保存所有 按钮,  然后在解决方案窗口中单击刷新图标, 然后,打开文件Resource.Designer.cs , 然后关闭文件Resource.Designer.cs。 如果还是不行,可以检查项目文件(XX.csproj,使用Notepad打开), 确保以下三行存在:

<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile>
<AndroidResgenClass>Resource</AndroidResgenClass>

关联Activity的前端UI页面

使用SetContentView(Resource.Layout.Main)将Activity类关联到前端页面。完成关联以后,可以通过FindViewById()获得页面中定义的控件。

        // 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++); };

Activity的特性MainLauncher=true,标识这个文件是应用的入口。

初始时代码如下:

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")]
    public 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 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++); };

            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‘]");
            }
        }
    }
}

关于页面跳转

在Layout中加入新Android Layout页面Second.axml, 在项目中加入新Activity类SecondActivity.cs。在Main页面,单击Button,然后跳转到Second页面,并且把参数传递过去。 创建新的Activity的实例是使用Intent,在Intent中把当前Activity的上下文传进去,使用SecondActivity类型初始化Intent,即var secondActivity = new Intent(this, typeof(SecondActivity));。  使用secondActivity.PutExtra()可以把参数传到second页, secondActivity.PutExtra("Arg1", "Argument from main page!");。启动该Intent,StartActivity(secondActivity);。 代码如下:

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

在second页的OnCreate方法中,使用Intent.GetStringExtra接受传递的参数。 代码如下:

[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;
    }
}
时间: 2024-08-07 21:03:48

Mono.Android 基础的相关文章

Android基础入门教程——10.12 传感器专题(3)——加速度-陀螺仪传感器

Android基础入门教程--10.12 传感器专题(3)--加速度/陀螺仪传感器 标签(空格分隔): Android基础入门教程 本节引言: 本节继续来扣Android中的传感器,本节带来的是加速度传感器(Accelerometer sensor)以及 陀螺仪传感器(Gyroscope sensor),和上一节的方向传感器一样有着x,y,z 三个轴, 还是要说一点:x,y轴的坐标要和绘图那里的x,y轴区分开来!传感器的是以左下角 为原点的!x向右,y向上!好的,带着我们的套路来学本节的传感器吧

Android基础入门教程——8.1.3 Android中的13种Drawable小结 Part 3

Android基础入门教程--8.1.3 Android中的13种Drawable小结 Part 3 标签(空格分隔): Android基础入门教程 本节引言: 本节我们来把剩下的四种Drawable也学完,他们分别是: LayerDrawable,TransitionDrawable,LevelListDrawable和StateListDrawable, 依旧贴下13种Drawable的导图: 1.LayerDrawable 层图形对象,包含一个Drawable数组,然后按照数组对应的顺序来

Android基础入门教程——8.1.2 Android中的13种Drawable小结 Part 2

Android基础入门教程--8.1.2 Android中的13种Drawable小结 Part 2 标签(空格分隔): Android基础入门教程 本节引言: 本节我们继续来学习Android中的Drawable资源,上一节我们学习了: ColorDrawable:NinePatchDrawable: ShapeDrawable:GradientDrawable!这四个Drawable~ 而本节我们继续来学习接下来的五个Drawable,他们分别是: BitmapDrawable:Insert

Android基础入门教程——2.3.12 Date &amp; Time组件(下)

Android基础入门教程--2.3.12 Date & Time组件(下) 标签(空格分隔): Android基础入门教程 本节引言: 本节我们来继续学习Android系统给我们提供的几个原生的Date & Time组件,他们分别是: DatePicker(日期选择器),TimePicker(时间选择器),CalendarView(日期视图),好吧, 其实一开始让我扣这几个玩意我是拒绝的,因为在我的印象里,他们是这样的: 简直把我丑哭了,有木有,终于知道为什么那么多人喜欢自定义这种类型的

Android基础入门教程——2.1 View与ViewGroup的概念

Android基础入门教程--2.1 View与ViewGroup的概念 标签(空格分隔): Android基础入门教程 本节引言: 告别了第一章,迎来第二章--Android中的UI(User Interface)组件的详解, 而本节我们要学习的是所有控件的父类View和ViewGroup类!突发奇想,直接翻译官方文档对 这两个东西的介绍吧,对了,天朝原因,google上不去,Android developer上不去,我们可以 改hosts或者用vpn代理,当然也可以像笔者一样使用国内的API

2015年最新Android基础入门教程目录(完结版)

2015年最新Android基础入门教程目录(完结版) 标签(空格分隔): Android基础入门教程 前言: 关于<2015年最新Android基础入门教程目录>终于在今天落下了帷幕,全套教程 共148节已编写完毕,附上目录,关于教程的由来,笔者的情况和自学心得,资源分享 以及一些疑问等可戳:<2015最新Android基础入门教程>完结散花~ 下面是本系列教程的完整目录: 第一章:环境搭建与开发相关(已完结 10/10) Android基础入门教程--1.1 背景相关与系统架构

《2015最新Android基础入门教程》完结散花~

<2015最新Android基础入门教程>完结散花~ 标签(空格分隔): 反思小结 引言: 从六月底就开始编写这套教程,历时将近五个多月,今天终于写完了,全套教程正文部分148篇, 十大章,从基本UI控件到四大组件,Intent,Fragment,事件处理,数据存储,网络编程,绘图与动画, 多媒体,系统服务等都进行了详细的讲解!代码都是都是在Android Studio上进行编写的,全文 采用Markdown,行文结构清晰,还结合了实际开发中一些常见的问题进行了剖析-由于个人能力的局限, 虽然

Android基础入门教程——10.10 传感器专题(1)——相关介绍

Android基础入门教程--10.10 传感器专题(1)--相关介绍 标签(空格分隔): Android基础入门教程 1.传感器相关介绍: 说到传感器,相信大家都不会陌生吧,比如微信的摇一摇就用到了加速度传感器: 传感器的定义:一种物理设备或者生物器官,能够探测.感受外界的信号,物理条件(如光,热, 适度)或化学组成(如烟雾),并将探知的信息传递给其他的设备或者器官! 传感器的种类:可以从不同的角度对传感器进行划分,转换原理(传感器工作的基本物理或化学 效应):用途:输出信号以及制作材料和工艺

Android基础入门教程——8.3.16 Canvas API详解(Part 1)

Android基础入门教程--8.3.16 Canvas API详解(Part 1) 标签(空格分隔): Android基础入门教程 本节引言: 前面我们花了13小节详细地讲解了Android中Paint类大部分常用的API,本节开始我们来讲解 Canvas(画板)的一些常用API,我们在Android基础入门教程--8.3.1 三个绘图工具类详解 中已经列出了我们可供调用的一些方法,我们分下类: drawXxx方法族:以一定的坐标值在当前画图区域画图,另外图层会叠加, 即后面绘画的图层会覆盖前