標籤:圖片 rom ttext pre 檔案 基於 訊息 cat short
本樣本基於網友現有安卓項目人肉翻譯,在Xamarin中替換和修改了很多方法的命名,比如某些屬性需要去掉getName的get首碼, 有些方法名稱需要使用Pascal命名法替換Java的Camel 命名規範
另外在內部類的使用方式上也有一些區別,但是整體上來說,大部分的方法名稱都與Java 原版Android一致,所以如果有現有的Android 項目需要轉換到Xamarin 還是很容易的.此處給Xamarin 66個贊
參考Java版本:http://blog.csdn.net/wxdjaqgs/article/details/44561101
博主提供了源碼下載
我所做的只是將需要的圖片檔案貼過來.然後開始翻譯代碼
由於IOS品台的打包流程還沒有去研究,所以本次實驗沒有使用ios 測試.以後有時間補上
PS:全篇無注釋(別問我,打死都不會說的 - -!)
貼代碼:
using System;using Android.App;using Android.Content;using Android.Widget;using Android.OS;namespace App3{ [Activity(Label = "App3", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { public static String ACTION_BTN = "com.example.notification.btn.login"; public static String INTENT_NAME = "btnid"; public const int INTENT_BTN_LOGIN = 1; static NotificationBroadcastReceiver mReceiver; public static NotificationManager NotifyManager; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); Button button = (Button)FindViewById(Resource.Id.btn_notification); button.Click += (sender, e) => { notification(); }; } private void intiReceiver() { mReceiver = new NotificationBroadcastReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.AddAction(ACTION_BTN); ApplicationContext.RegisterReceiver(mReceiver, intentFilter); } public void unregeisterReceiver() { if (mReceiver != null) { ApplicationContext.UnregisterReceiver(mReceiver); mReceiver = null; } } private void notification() { unregeisterReceiver(); intiReceiver(); RemoteViews remoteViews = new RemoteViews(PackageName, Resource.Layout.notification); remoteViews.SetTextViewText(Resource.Id.tv_up, "首都機場精品無線"); remoteViews.SetTextViewText(Resource.Id.tv_down, "已免費接入"); Intent intent = new Intent(ACTION_BTN); intent.PutExtra(INTENT_NAME, INTENT_BTN_LOGIN); PendingIntent intentpi = PendingIntent.GetBroadcast(this, 1, intent, PendingIntentFlags.UpdateCurrent); remoteViews.SetOnClickPendingIntent(Resource.Id.btn_login, intentpi); Intent intent2 = new Intent(); intent2.SetClass(this, typeof(MainActivity)); intent2.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask); PendingIntent intentContent = PendingIntent.GetActivity(this, 0, intent2, PendingIntentFlags.UpdateCurrent); Notification.Builder builder = new Notification.Builder(this); builder.SetOngoing(false); builder.SetAutoCancel(false); builder.SetContent(remoteViews); builder.SetTicker("正在使用首都機場無線"); builder.SetSmallIcon(Resource.Drawable.id_airport); Notification notification = builder.Build(); notification.Defaults = NotificationDefaults.Sound; notification.Flags = NotificationFlags.NoClear; notification.ContentIntent = intentContent; NotifyManager = (NotificationManager)GetSystemService(Context.NotificationService); NotificationManager notificationManager = NotifyManager; notificationManager.Notify(0, notification); } class NotificationBroadcastReceiver : BroadcastReceiver { public override void OnReceive(Context context, Intent intent) { String action = intent.Action; if (action.Equals(ACTION_BTN)) { int btn_id = intent.GetIntExtra(INTENT_NAME, 0); switch (btn_id) { case INTENT_BTN_LOGIN: Toast.MakeText(context, "從通知欄點登入", ToastLength.Short).Show(); if (mReceiver != null) { context.ApplicationContext.UnregisterReceiver(mReceiver); mReceiver = null; } NotificationManager notificationManager = NotifyManager; notificationManager.Cancel(0); break; } } } } }}
Xamarin 小試牛刀 通知欄訊息通知和按鈕(基於Java代碼人肉轉換)