UI調度:
?
?public class Dispatcher : Handler
??? {
??????? public override void HandleMessage(Message msg)
??????? {
??????????? var ai = msg.Obj as ActionItem;
??????????? if (ai != null)
??????????? {
??????????????? try
??????????????? {
??????????????????? ai.Result = ai.d.DynamicInvoke(ai.args);
??????????????? }
??????????????? catch (TargetInvocationException e)
??????????????? {
??????????????????? ai.Error = e.InnerException;
??????????????? }
??????????????? catch (Exception e)
??????????????? {
??????????????????? ai.Error = e;
??????????????? }
??????????????? if (ai.mre != null)
??????????????? {
??????????????????? ai.mre.Set();
??????????????? }
??????????? }
??????? }
??????? /// <summary>
??????? /// 同步調用
??????? /// </summary>
??????? public object Invoke(Delegate d, params object[] args)
??????? {
??????????? if (Java.Lang.Thread.CurrentThread().Equals(Looper.Thread))
??????????? {
??????????????? return d.DynamicInvoke(args);
??????????? }
??????????? var ai = new ActionItem
??????????? {
??????????????? d = d,
??????????????? args = args,
??????????????? mre = new System.Threading.ManualResetEvent(false)
??????????? };
??????????? SendMessage(new Message { Obj = ai });
??????????? ai.mre.WaitOne();
??????????? if (ai.Error != null)
??????????? {
??????????????? throw ai.Error;
??????????? }
??????????? return ai.Result;
??????? }
??????? /// <summary>
??????? /// 異步調用
??????? /// </summary>
??????? public void BeginInvoke(Delegate d, params object[] args)
??????? {
??????????? var msg = new Message()
??????????? {
??????????????? Obj = new ActionItem
??????????????? {
??????????????????? d = d,
??????????????????? args = args
??????????????? }
??????????? };
??????????? SendMessage(msg);
??????? }
??????? class ActionItem : Java.Lang.Object
??????? {
??????????? public Delegate d;
??????????? public object[] args;
??????????? public object Result;
??????????? public Exception Error;
??????????? public System.Threading.ManualResetEvent mre;
??????? }
??? }
?
String.xml中的樣式:
?
?
<?xml version="1.0" encoding="utf-8"?>
<resources>
???
? <style name="dialog" parent="@android:style/Theme.Dialog">
??? <item name="android:windowFrame">@null</item>
??? <item name="android:windowIsFloating">true</item>
??? <!--<item name="android:windowIsTranslucent">false</item>
??? <item name="android:windowNoTitle">true</item>
??? <item name="android:background">@android:color/black</item>
??? <item name="android:windowBackground">@null</item>
??? <item name="android:backgroundDimEnabled">false</item>-->
? </style>
?
</resources>
?
倒計時代碼:
?
??? public class TimerDialog
??? {
??????? #region 成員
??????? AlertDialog mDialog;
??????? Context mContext;
??????? int mId = 100;
??????? Dispatcher dp = new Dispatcher();
??????? Timer timer;
??????? public int Seconds { get; set; }
??????? #endregion
??????? #region 方法
??????? /// <summary>
??????? /// 初始化
??????? /// </summary>
??????? public TimerDialog(Context ctx)
??????? {
??????????? Seconds = 30;
??????????? mContext = ctx;
??????????? mDialog = new AlertDialog.Builder(new ContextThemeWrapper(mContext, Resource.Style.dialog)).Create();
??????????? TextView text = new TextView(mContext);
??????????? text.Id = mId;
??????????? text.TextSize = 120;
??????????? text.Text = Seconds.ToString();
??????????? //字體白色
??????????? text.SetTextColor(Android.Graphics.Color.White);
??????????? //居中
??????????? text.Gravity = GravityFlags.Center;
??????????? mDialog.SetView(text);
??????????? //阻止點擊別的地方導致對話框關閉
??????????? mDialog.SetCancelable(false);??
????? }
??
??????? /// <summary>
??????? /// 顯示對話框
??????? /// </summary>
??????? public void Show()
??????? {
??????????? mDialog.SetIcon(Android.Resource.Drawable.IcDialogInfo);
??????????? Start();
??????????? mDialog.Show();
??????? }
??????? /// <summary>
??????? /// 開始倒計時
??????? /// </summary>
??????? public void Start()
??????? {
??????????? Stop();
??????????? timer = new Timer(AFunction, null, 0, 1000);
??????? }
??????? /// <summary>
??????? /// 倒計時中
??????? /// </summary>
??????? private void AFunction(object obj)
??????? {
??????????? if (Seconds > 0)
??????????? {
??????????????? Seconds -= 1;
??????????????? dp.BeginInvoke(new Action(() =>
??????????????? {
??????????????????? (mDialog.FindViewById(mId) as TextView).Text = (Seconds + 1).ToString();
??????????????? }));
??????????? }
??????????? else
??????????? {
??????????????? Stop();
??????????????? dp.BeginInvoke(new Action(() =>
??????????????? {
?????????????????? mDialog.Dismiss();
?????????????????? mDialog.Dispose();
?????????????????? mDialog = null;
??????????????? }));
??????????? }
??????? }
??????? /// <summary>
??????? /// 停止倒計時
??????? /// </summary>
??????? public void Stop()
??????? {
??????????? if (timer != null)
??????????? {
??????????????? timer.Dispose();
??????????????? timer = null;
??????????? }
??????? }
??????? #endregion
??? }
?
?
Activity的調用:
?
?TimerDialog c = new TimerDialog(this);
??????????????? c.Show();
?
?