using System; using Android.Graphics.Drawables; using Android.Graphics; using Android.Telephony; using Android.Content; namespace Shared { /// /// UI处理类 /// public class Application { /// /// 获取实际的高度 /// /// The real height. /// Height. public static int GetRealHeight(int height) { return (int)(height / HeightScale); } /// /// 默认的字体大小 /// public static float FontSize = 12; /// /// 当前设备类型 /// public static readonly Device DeviceType = Device.Android; /// /// 获取实际的宽度 /// /// The real width. /// Width. public static int GetRealWidth(int width) { return (int)(width / WidthScale); } public static void HideSoftInput() { var imm = (Android.Views.InputMethods.InputMethodManager)Application.Activity.GetSystemService(Context.InputMethodService); if (Shared.EditText.Instance != null) { imm.HideSoftInputFromWindow(Shared.EditText.Instance.AndroidView.WindowToken, 0); //强制隐藏键盘 } else { imm.ToggleSoftInput(0, Android.Views.InputMethods.HideSoftInputFlags.NotAlways); } } /// /// 获取最小比例的实际值 /// /// The minimum real. /// Width or height. public static int GetMinReal(int widthOrHeight) { return (int)(widthOrHeight / MinScale); } /// /// 获取平均实际值 /// /// The minimum real average. /// Width or height. public static int GetMinRealAverage(int widthOrHeight) { return (int)(widthOrHeight / AverageScale); } public static void RunOnMainThread (Action action) { if (System.Threading.Thread.CurrentThread.ManagedThreadId == mainThreadId) { action (); } else { //System.Threading.Tasks.Task.Run (() => { Activity.RunOnUiThread (action); //}); }; } public static bool IsWifi; /// /// 当前设备的宽度 /// public static int CurrentWidth { get; internal set;} /// /// 当前设备的高度 /// public static int CurrentHeight { get; internal set; } /// /// 宽度比例 /// /// The width scale. public static float WidthScale { get { return DesignWidth * 1.0f / CurrentWidth; } } /// /// 高度比例 /// /// The height scale. public static float HeightScale { get { return DesignHeight * 1.0f / CurrentHeight; } } /// /// 最小比例 /// /// The minimum scale. public static float MinScale { get { return WidthScale < HeightScale ? WidthScale : HeightScale; } } /// /// 平均比例 /// /// The average scale. public static float AverageScale { get { return (WidthScale + HeightScale)/2; } } /// /// 设计宽度 /// public static int DesignWidth=640; /// /// 设计高度 /// public static int DesignHeight=1136; /// /// 当前的Activity /// public static Android.App.Activity Activity { get; internal set;} /// /// 当前主页 /// /// The main page. public static Shared.FrameLayout MainPage { get; private set; } static int mainThreadId; static Android.Widget.FrameLayout rootFrameLayout; /// /// 根容器视图 /// /// The root frame layout. public static Android.Widget.FrameLayout RootFrameLayout { get { return rootFrameLayout; } set { mainThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId; rootFrameLayout = value; rootFrameLayout.LayoutParameters.Width = CurrentWidth; rootFrameLayout.LayoutParameters.Height = CurrentHeight; MainPage = new FrameLayout(value) { Width = value.Width, Height = value.Height,BackgroundColor=0xFF000000, Parent = new RootView() { Width = value.Width, Height = value.Height } }; MainPage.Refresh(); } } /// /// 数据文件根目录 /// /// The root path. public static readonly string RootPath=System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)+"/"; /// /// 是否是pad /// public static bool IsPad { get { return false; //TelephonyManager telephony = (TelephonyManager)Activity.GetSystemService (Context.TelephonyService); //if (telephony.PhoneType == PhoneType.None) { // return true; //} else { // return false; //} } } /// /// 皮肤,null时表示用默认的 /// public static string Skin; /// /// GPS位置变化事件 /// public static Action LocationAction; public static bool IsGpsEnable = true; public static bool IsMusicEnable = true; /// /// 开启定位服务 /// public static void StartGPSLocationService() { Activity.StartService(new Intent(Activity, typeof(GPSLocationService))); } /// /// 关闭定位服务 /// public static void StopGPSLocationService() { Activity.StopService(new Intent(Activity, typeof(GPSLocationService))); } } }