Mono.Android 基础
Mono.Android 基础 (地址)
Mono.Android项目结构是
— Project
+ Assets
+ Resources
+ drawable
+ layout
+ values
Resource.Designer.cs
XXActivity.cs
其中, Layout文件夹下存放App的前端UI文件,前端UI是一个后缀名为.axml的XML文件,该文件有两个视图:Design和Source。在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;
}
}
Mono.Android 基础的更多相关文章
- 【Xamarin开发 Android 系列 4】 Android 基础知识
原文:[Xamarin开发 Android 系列 4] Android 基础知识 什么是Android? Android一词的本义指“机器人”,同时也是Google于2007年11月5日宣布的基于Li ...
- Android基础测试题(四)
看了前两道题大家有没有发现,测试题少了(一),大家猜猜测试题(一)是什么? Android基础测试题(四): 需求: 建一个方法,格式化输出2016-11-14 10:15:26格式的当前时间,然后截 ...
- Android基础测试题(二)
今天给大家带来的是Android基础测试题(二) 题目要求: 定义一个5位长度的整型数组并初始化,然后构建方法根据用户传入的数字判断是否存在数组中,如果存在,返回所在位置,如果不存在,返回-1 首先第 ...
- 深入理解gradle编译-Android基础篇
深入理解gradle编译-Android基础篇 导读 Gradle基于Groovy的特定领域语言(DSL)编写的一种自动化建构工具,Groovy作为一种高级语言由Java代码实现,本文将对Gradle ...
- android基础---->JSON数据的解析
上篇博客,我们谈到了XML两种常用的解析技术,详细可以参见我的博客(android基础---->XMl数据的解析).网络传输另外一种数据格式JSON就是我们今天要讲的,它是比XML体积更小的数据 ...
- Xamarin.Android提示找不到mono.Android.Support.v4
Xamarin.Android提示找不到mono.Android.Support.v4 错误信息:Error: Exception while loading assemblies: System.I ...
- 基础4 Android基础
基础4 Android基础 1. Activity与Fragment的生命周期. Activity生命周期 打开应用 onCreate()->onStart()->onResume 按BA ...
- Android基础总结(8)——服务
服务(Service)是Android中实现程序后台运行的解决方案,它非常适合用于去执行哪些不需要和用户交互而且还要长期运行的任务.服务的运行不依赖任何用户界面,即使当程序被切换到后台,或者用户打开了 ...
- Android基础_web通信3
在Android基础_web通信2中,我运用的JSONObject是Android原生的json类,通过import org.json.JSONObject来导入. 还有另外一种更简单的方法,就是用G ...
随机推荐
- 【思路】-分页-双top分页算法的原理
描述:实现分页的一种算法 大致过程:访客访问不同的分页,为这个当前页生成动态的查询SQL,然后送到数据库中执行 输入:总条数,每页多少条,第几页,查询的SQL,排序的字段 注意:传入的排序字段需要构成 ...
- Ext4 ComboBox组件使用
先来看例子: Ext.define('schoolModel', { extend: 'Ext.data.Model', fields: [{ name: 'id', type: 'string' ...
- [css3]跑马灯
<div class="marquee"> <div> <p>纯CSS3生成的走马灯效果</p> <p>纯CSS3生成的 ...
- 【DIY】【外壳】木板 & 亚克力 加工
—————————————————————————————————————————————————————————————————————— 一.途径 淘宝 https://item.taobao.c ...
- tensor flow入门笔记
个人学习笔记,欢迎交流.
- byte数据的常用操作函数[转发]
/// <summary> /// 本类提供了对byte数据的常用操作函数 /// </summary> public class ByteUtil { ','A','B',' ...
- NPOI 2.0 读取、编辑、保存Excel文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- python 异常处理学习笔记
搬运至慕课网,精华截图,视频链接在这 : http://www.imooc.com/learn/457 1. 异常检查目的 2. python 可能出现的异常 3. 异常的处理过程 try - ex ...
- 关于u盘启动,关于UEFI,关于hp手提计算机
这个国庆前夕,遇到点麻烦了:一台新的手提计算机,按照常规方法不能用u盘启动引导.导致也无法做备份.所以,研究了不少小时哦...终于也可以解决的. 关于u盘启动,一般常用的有:u大侠(推荐),大白菜(不 ...
- Spark源码学习1.7——Master.scala
master第一步是加载系统定义的环境变量,如worker的超时时间.系统保留的Application数目等:第二步,加载worker的信 息,地址.id等:第三步,加载Application的信息, ...