与“原生”安卓开发不同的地方
-
程序入口:Activity属性标签设置了MainLauncher = true的Activity
-
AndroidManifest.xml 位于Properties文件夹下
-
view的事件处理直接使用委托
-
获取view的方式:使用泛型(见上图Button)
-
Java的getter、setter被翻译成字段
-
Intent-filter 在属性标签中设置
[Activity(Label = "IntentFilterDemoActivity")] [IntentFilter(new[] { Intent.ActionGetContent }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = "vnd.android.cursor.item/vnd.google.note")] public class IntentFilterDemoActivity : Activity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Create your application here } }
生成的xml如下:
<activity android:label="ResultDemoActivity" android:name="md5c6d828a91b42289b41de7c26628c84e9.ResultDemoActivity"> <intent-filter> <action android:name="android.intent.action.GET_CONTENT" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> </intent-filter> </activity>
-
在xml中定义的事件处理方法,需要加上[Java.Interop.Export("DoClick")]
[Java.Interop.Export("DoClick")] public void DoClick(View view) { Toast.MakeText(this, "Clikced", ToastLength.Short).Show(); }
-
自定义Application类
在java里要只向AndroidManifest.xml里添加一句:<application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".MyApplication">
在Xamarin里,首先你要创建一个具有两个特殊参数的构造器的自定义Application类,同时要还加上[Application]的特性。
[Application] public class MyApplication : Application { public MyApplication(IntPtr handle, JniHandleOwnership ownerShip) : base(handle, ownerShip) { } }
. . . . . .