一步一步将你的C#程序部署到Android.该如何解决

一步一步将你的C#程序部署到Android.......
本帖最后由 hyblusea 于 2013-07-30 22:11:44 编辑
C#是一种优秀的编程语言,语法之优雅,代码之简洁使得众多软粉多年来对她不离不弃。 但是如何将C#程序部署到Linux, Android等平台,这当然是得依靠众所周知的Mono。

本文Demo程序比较简单,实现了通过HttpRequest 查询天气,最终效果如下:
一步一步将你的C#程序部署到Android.该如何解决


1. 下载并安装 Xamarin
http://xamarin.com/download
注册Xamarin账号, Role 选择 Academic(学者)即可;
运行 XamarinInstaller.exe在线安装程序,需要一个良好的网络环境,所需的安装程序体积大约 1.44G ,安装过程一路默认即可。

2.破解
网上找的破解:http://download.csdn.net/detail/flydoos/5820325
解压缩后,将文件覆盖到指定位置即可。

3.开发
开发Mono Android程序可以使用Xamarin Studio,也可以使用Visual Studio,建议使用VS,因为Xamarin对VS提供有强大的插件 再配合VS本身强大的功能,会使开发工作如鱼得水,另外Xamarin Studio目前还不够完善,比如添加引用 之后,需要重启。 

a.创建 Android项目 ,如下图:
一步一步将你的C#程序部署到Android.该如何解决

b.项目文件结构,如下图:
一步一步将你的C#程序部署到Android.该如何解决

c.页面文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1"
        android:layout_marginBottom="5dip"
        android:layout_marginLeft="5dip"
        android:layout_marginRight="5dip"
        android:layout_marginTop="5dip">
        <TextView
            android:id="@+id/LblCity"
            android:text="@string/PressCity"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
        <EditText
            android:id="@+id/TxtWeather"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:lines="1" />
    </LinearLayout>

    我们可以看出,所有的控件均放在LinearLayout中,这是一个部局控件,LinearLayout又分为水平布局和垂直布局,比如一行中需要放置多个控件,这时候就需要用到水平布局。
d. cs文件
    我们所熟悉的C#,将在这里大展拳脚,一切看上去都是那么亲切。

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.IO;


namespace AndroidHelloWorld
{
[Activity(Label = "EasyWeather", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
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);
EditText txtWeather = FindViewById<EditText>(Resource.Id.TxtWeather);

            // 天气查询结果
            TextView lblCity = FindViewById<TextView>(Resource.Id.LblCityRst);                  // 城市 
            TextView lblCurTmp = FindViewById<TextView>(Resource.Id.LabCurTempRst);             // 当前温度  
            TextView lblWeather = FindViewById<TextView>(Resource.Id.LabWeatherRst);            // 天气
            TextView lblRange = FindViewById<TextView>(Resource.Id.LabRangeRst);                // 范围
            TextView lblUptTime = FindViewById<TextView>(Resource.Id.LabUptTimeRst);            // 更新时间