Xamarin 小試牛刀 通知欄訊息通知和按鈕(基於Java代碼人肉轉換)

來源:互聯網
上載者:User

標籤:圖片   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代碼人肉轉換)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.