zoukankan      html  css  js  c++  java
  • mono for android 2 (通过Hello,World ,熟悉项目)

    一. 在vs2012中新建项目,选择Andriod Application的默认项目模板:

     

    二. 查看项目结构:

    其实基本跟silverlight/wpf相识,主要结构:

    1. Assets 和 Resource 目录:

    Assets 目录, 如果应用需要用到二进制资源文件, 比如特殊字体、声音等, 放在这个目录下, 并将 BuildAction 设置为 AndrioidAsset , 资源将会和应用程序一起部署, 在运行时可以通过 AssetManager 使用类似下面的代码进行访问:

    public class ReadAsset : Activity
    {
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            InputStream input = Assets.Open ("my_asset.txt");
        }
    }
    

     另外,字体文件可以这样加载:

    Typeface tf = Typeface.CreateFromAsset(
        Context.Assets, "fonts/samplefont.ttf");
    

     Resource 目录, 包含应用程序所需的图片、 布局描述、 二进制文件和字符串字典等资源文件。 比如, 一个简单的 Android 应用包含一个界面描述文件 (main.axml) , 一个国际化的字符串字典 (strings.xml) 以及图标 (icon.png) , 这些文件按照下面的结构保存在 “Resource” 目录内:

    Resources/
       drawable/
          icon.png
       layout/
          main.axml
       values/
          strings.xml
    

    为了让编译系统能够将资源文件识别出 Android 资源, 需要将其编译动作 (Build Action) 设置为 “Android Resource”。 上面的目录结构经过编译之后, 将会生成类似下面的文件:

    public class Resource {
    
        public class Drawable {
            public const int icon = 0x123;
        }
    
        public class Layout {
            public const int main = 0x456;
        }
    
        public class Strings {
            public const int FirstString = 0xabc;
            public const int SecondString = 0xbcd;
        }
    }
    

     使用 Resource.Drawable.icon 可以引用 drawable/icon 文件, Resource.Layout.main 可以引用 /layout/main.axml 文件, 而使用 Resource.Strings.FirstString 则可以引用 values/strings.xml 文件中的第一个字符串。

    以上这些和 Android SDK 文档中介绍的都是大同小异的,加上了net 特有的一些风格而已。

    2. Activity 及 View:

    项目中默认生成Main.axml,这是默认的View,你可自己根据实际需要新增或修改等。

    与winform/wpf等不同的是 android项目中并没有main的主函数,也没有StartupUri的启动界面,那程序是如何启动主界面呢,

    查看Activity1的代码中不难发现,有几点需要注意的:

     [Activity(Label = "HelloWorld", MainLauncher = true, Icon = "@drawable/icon")]
        public class Activity1 : 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++); };
            }
        }
    

     第一点:[Activity(Label = "HelloWorld", MainLauncher = true, Icon = "@drawable/icon")]中指定了Label,并指定了MainLauncher=true,也就是说这是主程序,启动时候触发的Activity,至于具体对应哪个界面的,可以从SetContentView(Resource.Layout.Main) 这一句看到Activity对应得界面。

    第二点:  // 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++); };

    这两句也充分的展示了在mono for android中基于事件的机制也是支持的,只是需要简单的为控件添加相应的委托事件即可,显得格外简单。

    3 .项目属性

    项目属性主要是针对android目标系统版本/支持的架构,程序名称等的设置,这在发布和部署的时候应格外注意。

    好了,简单运行一下HelloWorld的程序,效果就出来了,第一节的介绍就到这里了。

  • 相关阅读:
    Json-lib使用 转载
    利用PinYin4j 实现List中的对象按数字,字母, 汉字排序
    web 模板 类似京东左侧的导航栏
    常见挂马方式
    JS挂马攻防
    代码中的202.102.100.100
    利用Jquery获取、设置iframe中元素
    [PHP]基本排序(冒泡排序、快速排序、选择排序、插入排序、二分法排序)
    js验证输入的金钱格式
    js获取当前对象的颜色判断改变颜色
  • 原文地址:https://www.cnblogs.com/wuyansheng/p/3355582.html
Copyright © 2011-2022 走看看