Mono for Android 示例项目教程

Mono for Android 示例项目教程

monodroid-samplesA collection of Xamarin.Android sample projects.项目地址:https://gitcode.com/gh_mirrors/mo/monodroid-samples

1. 项目的目录结构及介绍

Mono for Android 示例项目的目录结构如下:

monodroid-samples/
├── HelloWorld/
│   ├── HelloWorld/
│   │   ├── MainActivity.cs
│   │   ├── Main.axml
│   │   ├── Properties/
│   │   │   ├── AndroidManifest.xml
│   │   │   ├── AssemblyInfo.cs
│   │   ├── Resources/
│   │   │   ├── drawable/
│   │   │   ├── layout/
│   │   │   ├── values/
│   │   │   ├── Resource.designer.cs
│   ├── HelloWorld.sln
├── MyFirstApp/
│   ├── MyFirstApp/
│   │   ├── MainActivity.cs
│   │   ├── Main.axml
│   │   ├── Properties/
│   │   │   ├── AndroidManifest.xml
│   │   │   ├── AssemblyInfo.cs
│   │   ├── Resources/
│   │   │   ├── drawable/
│   │   │   ├── layout/
│   │   │   ├── values/
│   │   │   ├── Resource.designer.cs
│   ├── MyFirstApp.sln
├── ...

目录结构介绍

  • HelloWorld/MyFirstApp/ 是示例项目的根目录。
  • 每个示例项目包含一个解决方案文件(如 HelloWorld.sln)和一个项目文件夹(如 HelloWorld/)。
  • 项目文件夹中包含主要的代码文件(如 MainActivity.cs)和布局文件(如 Main.axml)。
  • Properties/ 目录包含项目的配置文件(如 AndroidManifest.xml)和程序集信息文件(如 AssemblyInfo.cs)。
  • Resources/ 目录包含项目的资源文件,如图片(drawable/)、布局(layout/)和字符串(values/)。

2. 项目的启动文件介绍

每个示例项目的启动文件是 MainActivity.cs,它继承自 Android.App.Activity 类,并重写了 OnCreate 方法。以下是一个典型的 MainActivity.cs 文件的内容:

using Android.App;
using Android.OS;
using Android.Widget;

namespace HelloWorld
{
    [Activity(Label = "HelloWorld", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // 设置布局文件
            SetContentView(Resource.Layout.Main);

            // 获取按钮并设置点击事件
            Button button = FindViewById<Button>(Resource.Id.MyButton);
            button.Click += delegate {
                button.Text = "Hello, Xamarin!";
            };
        }
    }
}

启动文件介绍

  • [Activity(Label = "HelloWorld", MainLauncher = true, Icon = "@drawable/icon")] 属性定义了活动的标签、图标和是否作为主启动器。
  • OnCreate 方法在活动创建时调用,用于设置布局文件和初始化控件。

3. 项目的配置文件介绍

每个示例项目的配置文件主要位于 Properties/ 目录下,最重要的配置文件是 AndroidManifest.xml。以下是一个典型的 AndroidManifest.xml 文件的内容:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="HelloWorld"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="15" />
    <application android:label="HelloWorld" android:icon="@drawable/icon">
        <activity android:name=".MainActivity"
                  android:label="HelloWorld">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

配置文件介绍

monodroid-samplesA collection of Xamarin.Android sample projects.项目地址:https://gitcode.com/gh_mirrors/mo/monodroid-samples

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

裴才隽Tanya

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值