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 ...
随机推荐
- 浅析final 关键字
谈到final关键字,想必很多人都不陌生,在使用匿名内部类的时候可能会经常用到final关键字.另外,Java中的String类就是一个final类,那么今天我们就来了解final这个关键字的用法.下 ...
- oracle Net Manager 服务命名无法配置(无法新建、添加服务名)
今天在配置oracle Net Manager 配置服务命名的时候总是无法配置,以前配置的服务名都显示不出来,然后点击绿色添加按钮也没反应,因为先前我修改了oracle\product\10.2.0\ ...
- Python中的深浅拷贝
1.什么是深浅拷贝? python中一切皆对象,python中的数字.字符串.元组等,如果存放在了内存中,这部分内存里面的内容是不会改变的,但是也有情况,内存中存放了可变对象,比如说列表和字典,他们的 ...
- linux 硬盘速度测试
[root@iZ25oat874uZ data]# time dd if=/dev/zero of=/var/test bs=8k count=1000000 1000000+0 records in ...
- php实现递归的三种方式: 遍历文件夹实例
递归函数是我们常用到的一类函数,最基本的特点是函数自身调用自身,但必须在调用自身前有条件判断,否则无限无限调用下去.实现递归函数可以采取什么方式呢?本文列出了三种基本方式.理解其原来需要一定的基础知识 ...
- .net 附加W3wp进程
步骤:1.鼠标右键点击application项目中的web,找到属性 2.修改web服务器地址(如果使用localhost报错,则http://电脑IP/): 3.在本地iis中找到你部署的网站,点击 ...
- [移动端]rem适配
原理:给html根节点设置一个基础font-size值,然后页面的所有元素布局均相对于该font-size值采用rem单位设定.font-size的取值通过js计算. 但字体不用rem单位,原因如下: ...
- [css3]叉叉旋转效果
.close_frame{display:inline-block;height:14px;width:14px;background:url("../images/closeiframe. ...
- 【 2013 Multi-University Training Contest 8 】
HDU 4678 Mine 对于每个空白区域,求SG值. 最后异或起来等于0,先手必败. #pragma comment(linker,"/STACK:102400000,102400000 ...
- Cookie与Session用法
以下是Cookie的完整实例 MyEclipse新建web Project工程,建两个jsp文件,如下 1.login.jsp <%@ page language="java" ...