
1.废话背景介绍 html
在Build 2016开发者大会上,微软宣布,Xamarin将被整合进全部版本的Visual Studio之中。android
这也就是说,Xamarin将免费提供给全部购买了Visual Studio的开发者使用。这样一来,开发者就能利用 .NET和C#工具,开发Android和iOS应用程序了。
重要的是,Xamarin与Visual Studio的整合没有任何限制。尽管未被收购前,Xamarin也提供免费版给开发者使用,但该免费版只支持小型可执行程序。想要在Xamarin上开发大型可执行程序的话,开发者仍是得先花钱买受权。
但如今状况不一样了,开发者除了没法自由使用Xamarin部分面向企业客户的功能,其它全部工具均可无偿使用。
重要的是,如今Xamarin Studio还支持在OS X平台上使用。
此外微软表示,几周后将彻底开放Xamarin SDK。这意味着Xamarin的运行环境、库文件界面和编码工具,将被公布在Github上,由 .NET Foundation进行管理。与此同时,供开发者构建用户界面的跨平台工具包Xamarin Forms也将开源。
前面都是科普废话,上正文.ide
2.环境安装 工具
抱着试一试的心态,更新了VS 2015 Updata2, 由于之前就玩过Xamarin,注册了账号发现升级后仍是使用过去,很郁闷 觉得微软忽悠咱们呢.布局
试着找找 无心中在Tools --> Options --> Other 里面看到 Xamarin for Visual Studio Updataspost
咦这是什么鬼?ui
点击 Check Now 试试 如图:this

等到一段时间, 墙的威力太大,有必要的话请FQ更新.另外Android SDK NDK的安装(什么,你知道怎么安装,请baidu一下)也请FQ,下载镜像能够设置为东软的镜像 mirrors.neusoft.edu.cn编码
如图:spa

这些下载更新快多了.
环境这些准备好了.
咱们来回归正题.
3.Android底部导航条
终于能够写代码了,开始不了解Xamarin Android的原理,还导出找教程,尼玛最后才知道无论Android仍是IOS Xamarin均可以直接使用原生的界面布局加C#语法编译打包.这下简单了找个原生的例子直接使用,略加翻译Java-->C#便可,Java C#类似度极高. 聪明的大家确定一看就会.如下实例本人均翻译至Java的实例.
简单说说原理,主要使用TabActivity TabHost Intent. 你问我他们分别是干什么的? 呃!~~~ 这个能够将一本书了, 用了你就知道了.
如今 TabActivity听说已通过时 Fragment已取代了他.有兴趣的朋友能够试试,找个例子改改.
TabActivity在API 13(Android 3.2)被标记为过时,须要使用Fragment来实现,Fragment是Android 3.0引入的一个概念,主要就是为了适应各类不一样的屏
先上张效果图,这是最终要实现的样子:

开始建工程

这个你们都会吧,很少说.
建立Maintabs.axml
xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0" />
<TabWidget
android:id="@android:id/tabs"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0" />
<RadioGroup
android:gravity="center_vertical"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:id="@+id/main_radio"
android:background="@drawable/maintab_toolbar_bg"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radio_button0"
android:layout_marginTop="2.0dip"
android:text="@string/main_home"
android:drawableTop="@drawable/icon_1_n"
style="@style/main_tab_bottom" />
<RadioButton
android:id="@+id/radio_button1"
android:layout_marginTop="2.0dip"
android:text="@string/main_news"
android:drawableTop="@drawable/icon_2_n"
style="@style/main_tab_bottom" />
<RadioButton
android:id="@+id/radio_button2"
android:layout_marginTop="2.0dip"
android:text="@string/main_manage_date"
android:drawableTop="@drawable/icon_3_n"
style="@style/main_tab_bottom" />
<RadioButton
android:id="@+id/radio_button3"
android:layout_marginTop="2.0dip"
android:text="@string/main_friends"
android:drawableTop="@drawable/icon_4_n"
style="@style/main_tab_bottom" />
<RadioButton
android:id="@+id/radio_button4"
android:layout_marginTop="2.0dip"
android:text="@string/more"
android:drawableTop="@drawable/icon_5_n"
style="@style/main_tab_bottom" />
RadioGroup>
LinearLayout>
TabHost>
修改 MainActivity.cs
1 using System;
2 using Android.App;
3 using Android.Content;
4 using Android.Runtime;
5 using Android.Views;
6 using Android.Widget;
7 using Android.OS;
8
9 namespace MyAppTest
10 {
11 [Activity(Label = "MyAppTest", MainLauncher = true, Icon = "@drawable/icon",Theme = "@android:style/Theme.DeviceDefault.NoActionBar")]
12 public class MainTabActivity :
TabActivity, CompoundButton.IOnCheckedChangeListener
13 {
14 private TabHost mTabHost;
15 private Intent mAIntent;
16 private Intent mBIntent;
17 private Intent mCIntent;
18 private Intent mDIntent;
19 private Intent mEIntent;
20
21 protected override void OnCreate(Bundle bundle)
22 {
23 base.OnCreate(bundle);
24 RequestWindowFeature(WindowFeatures.NoTitle);
25 // Set our view from the "main" layout resource
26 SetContentView(Resource.Layout.
Maintabs);
27
28 this.mAIntent = new Intent(this, typeof (AActivity));
29 this.mBIntent = new Intent(this, typeof (BActivity));
30 this.mCIntent = new Intent(this, typeof (CActivity));
31 this.mDIntent = new Intent(this, typeof (DActivity));
32 this.mEIntent = new Intent(this, typeof (EActivity));
33
34 ((RadioButton)FindViewById(Resource.Id.radio_button0)).SetOnCheckedChangeListener(this);
35 ((RadioButton)FindViewById(Resource.Id.radio_button1)).SetOnCheckedChangeListener(this);
36 ((RadioButton)FindViewById(Resource.Id.radio_button2)).SetOnCheckedChangeListener(this);
37 ((RadioButton)FindViewById(Resource.Id.radio_button3)).SetOnCheckedChangeListener(this);
38 ((RadioButton)FindViewById(Resource.Id.radio_button4)).SetOnCheckedChangeListener(this);
39
40
41 SetupIntent();
42 }
43
44 public void OnCheckedChanged(CompoundButton buttonView, bool isChecked)
45 {
46 if (isChecked)
47 {
48 switch (buttonView.Id)
49 {
50 case Resource.Id.radio_button0:
51 this.mTabHost.SetCurrentTabByTag("A_TAB");
52 break;
53 case Resource.Id.radio_button1:
54 this.mTabHost.SetCurrentTabByTag("B_TAB");
55 break;
56 case Resource.Id.radio_button2:
57 this.mTabHost.SetCurrentTabByTag("C_TAB");
58 break;
59 case Resource.Id.radio_button3:
60 this.mTabHost.SetCurrentTabByTag("D_TAB");
61 break;
62 case Resource.Id.radio_button4:
63 this.mTabHost.SetCurrentTabByTag("MORE_TAB");
64 break;
65 }
66 }
67 }
68
69 private void SetupIntent()
70 {
71 this.mTabHost = this.TabHost;
72 TabHost localTabHost = this.mTabHost;
73
74 localTabHost.AddTab(BuildTabSpec("A_TAB", Resource.String.main_home,Resource.Drawable.icon_1_n, this.mAIntent));
75
76 localTabHost.AddTab(BuildTabSpec("B_TAB", Resource.String.main_news,
77 Resource.Drawable.icon_2_n, this.mBIntent));
78
79 localTabHost.AddTab(BuildTabSpec("C_TAB",
80 Resource.String.main_manage_date, Resource.Drawable.icon_3_n,
81 this.mCIntent));
82
83 localTabHost.AddTab(BuildTabSpec("D_TAB", Resource.String.main_friends,
84 Resource.Drawable.icon_4_n, this.mDIntent));
85
86 localTabHost.AddTab(BuildTabSpec("MORE_TAB", Resource.String.more,
87 Resource.Drawable.icon_5_n, this.mEIntent));
88
89 }
90
91 private TabHost.TabSpec BuildTabSpec(string tag, int resLabel, int resIcon, Intent content)
92 {
93 return this.mTabHost.NewTabSpec(tag).SetIndicator(GetString(resLabel),
94 Resources.GetDrawable(resIcon)).SetContent(content);
95 }
96 }
97 }
注意红色部分,与自动建立的不一样.
建立其余 Activity.cs / AActivity BActivity CActivity DActivity EActivity
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace MyAppTest
{
[Activity(Label =
"
AActivity
", Icon =
"
@drawable/icon
", Theme =
"
@android:style/Theme.DeviceDefault.NoActionBar
")]
public
class AActivity : Activity
{
protected
override
void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
RequestWindowFeature(WindowFeatures.NoTitle);
TextView tv =
new TextView(
this);
tv.SetText(
"
This is A Activity!
", TextView.BufferType.Normal);
tv.Gravity = GravityFlags.Center;
SetContentView(tv);
}
}
}
涉及到的资源这里没有列出,请参考源码.
4.真机调试
代码写完了,该编译如下了, 看看是否有错, 编译顺利经过, 插上真机(我这里有个买车险送的 200块的 Android平板 准备扔了的,试了下很真能够调试程序),
要VS直接调试的话,须要VS是以管理员方式启动的, 查上设备后你会看到:
设备显示在调试的位置,直接F5就能够了. 什么?无图无真相?
运行效果:
5.打包
调试完了,该打包发布了是吧?
等等 打包(Export Android Package(.apk))怎么是灰色的?

别着急,大微软确定不会忽悠你.
原来你须要把编译 模式选择为 Release,默认的Debug是不能打包的. 看这里. 看这里.
打包完成:

你问我卡不卡呀? 我这不到200块钱的白送的pad均可以流畅的跑.
结束了,该上码了:
本文源码下载:
http://files.cnblogs.com/files/crazybird/MyAppTest.zip
