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 ...
随机推荐
- JAVA 多线程随笔 (一) 可见性和volatile关键字
// 先上代码 1 public class NoVisibility { private static boolean ready; private static int number; priva ...
- KETTLE 配置资源库
KETTLE 是一款开源的ETL工具,通过图形界面进行设计,可以对数据进行转换.设计好的文件分为两类,一类是trans,一类是job,这些文件可以存储到文件系统中. 也可以存储到数据库中. 如 ...
- 立体角的单位——立体弧度(sr)
国际单位制(SI)中,关于物理量 发光强度 的介绍: 1cd(坎德拉)为一光源在给定方向的发光强度,该光源发出频率为540×1012Hz(赫兹)的单色辐射,且在此方向上的辐射强度为 1/683 W/s ...
- String字符串类课后作业
String动手动脑和课后作业 请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? 结果: 总结:在Java中,内容相同的字串常量(&quo ...
- ORCALE复制表结构
1.oracle 复制表结构 不要内容 create table 表1 as select * from 表2 where 1=2 2.oracle 复制表结构 要内容 create table 表1 ...
- Android 中的常用方法
//安装apk文件 private void installAPK(File file) { Intent intent = newIntent(Intent.ACTION_VIEW); Uri ...
- POJ 3294 二分找超过一半字符串中存在的子串
题目大意: 给定n个字符串,求出现在不小于k/2个字符串中的最长子串. 二分找对应子串长度的答案,将所有字符串链接成一个长字符串求后缀数组,记录每一个位置本属于第几个字符串,利用height查询的时候 ...
- 微信APP支付服务端开发Java版(一)
一.准备工作 去微信开发者中心下载(扫码支付,里面的大部分代码是可以用的) https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=11 ...
- curl获取远程图片存到本地
$url = 'http://sssss/sss/xu0fLo9waqKSTDO7j0kSO41O5Luq3LB6ozUvY4O7OsXUWNicB49fBs8nGYzoqcwGDARQZHpVuic ...
- 在eclipse的maven插件中搜寻本地仓库中的jar搜索不到的解决方案
在eclipse的maven插件中搜寻本地仓库中的jar搜索不到的解决方案 之前,用过maven管理项目的童鞋都知道本地会有一个${User_Home}.m2/repository仓库 是用来存放ja ...