using System;
using UIKit;
namespace Shared
{
//AVFoundation CoreGraphics UIKit CoreMedia AudioToolbox VideoToolbox GLKit OpenAL MobileCoreServices SystemConfiguration CoreTelephony AVFoundation WebKit
///
/// UI处理类
///
public static class Application
{
//public static double PhoneType
//{
// get
// {
// var type = UIDevice.CurrentDevice.SystemVersion.Split(new string[] { "." }, StringSplitOptions.None)[0];
// var d = 1.0d;
// double.TryParse(type, out d);
// return d;
// }
//}
public enum GpsUseMode
{
Always,
WhenInUse
}
public static bool IsGpsEnable = true;
public static GpsUseMode CurrentGpsUseMode = GpsUseMode.WhenInUse;
public static bool IsMusicEnable = true;
///
/// EditText是否默认密码输入方式
///
public static bool IsEditTextContentTypePassword = true;
///
/// 2020-06-23
/// 是否全屏幕手机
///
public static bool IsFullScreen
{
get
{
/// iPhoneX_XS_11Pro 812
/// iPhoneXSMax_XR_11_11ProMax 896
/// 是否全屏幕手机 需要持续更新适配
//HDLUtils.WriteLine("UIScreenWidth:" + UIScreen.MainScreen.Bounds.Size.Width + " UIScreenHeight:" + UIScreen.MainScreen.Bounds.Size.Height);
return (UIScreen.MainScreen.Bounds.Size.Width == 375 && UIScreen.MainScreen.Bounds.Size.Height == 812) || (UIScreen.MainScreen.Bounds.Size.Width == 414 && UIScreen.MainScreen.Bounds.Size.Height == 896);
}
}
//2019-08-14 修改
public static double PhoneType
{
get
{
var device = Xamarin.Essentials.DeviceInfo.Model;
if (device == null)
{
return 1.0;
}
var type = device.Replace("iPhone", "").Split(',')[0];
double d = 1.0;
double.TryParse(type, out d);
return d;
}
}
//public static String PhoneTypeModel
//{
// get
// {
// var device = UIDevice.CurrentDevice.Model;// .SystemVersion.Split(new string[] { "." }, StringSplitOptions.None)[0];
// return device;
// }
//}
///
/// 获取实际的高度
///
/// The real height.
/// Height.
public static int GetRealHeight(int height)
{
return (int)(height / HeightScale);
}
///
/// 默认的字体大小
///
public static int FontSize = 18;
public static bool IsUsePingFang = false;
///
/// 获取实际的宽度
///
/// The real width.
/// Width.
public static int GetRealWidth(int width)
{
return (int)(width / WidthScale);
}
///
/// 隐藏软键盘
///
public static void HideSoftInput()
{
if (Shared.EditText.Instance != null)
{
Shared.EditText.Instance.Foucs = false;
}
}
///
/// 当前设备类型
///
public static readonly Device DeviceType = Device.Ios;
///
/// 获取最小比例的实际值
///
/// 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);
}
///
/// 主线程执行指定事件
///
/// Action.
public static void RunOnMainThread(Action action)
{
try
{
if (System.Threading.Thread.CurrentThread.ManagedThreadId == mainThreadId)
{
action?.Invoke();
}
else
{
RootFrameLayout.InvokeOnMainThread(action);
}
}catch(Exception ex)
{
RunOnMainThread(() =>
{
try
{
CommonClass.ExcptionEvent(ex);
}catch(Exception ex2)
{
Console.WriteLine($"主线程异常:{ex2.Message}");
}
});
}
}
public static bool IsWifi {
get {
return MyReachability.InternetConnectionStatus() == MyNetworkStatus.ReachableViaWiFiNetwork;
}
}
///
/// 当前设备的宽度
///
public static int CurrentWidth;
///
/// 当前设备的高度
///
public static int CurrentHeight;
///
/// 宽度比例
///
/// The width scale.
public static float WidthScale
{
get
{
return DesignWidth * 1.0f / CurrentWidth;
}
}
///
/// 高度比例
///
/// The height scale.
public static float HeightScale
{
get
{
//return Application.CurrentHeight / bigsize;
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=1920;
///
/// 设计高度
///
public static int DesignHeight=1136;
// CoreGraphics.CGRect screenRect = [[UIScreen mainScreen] bounds];
//CoreGraphics.CGSize screenSize = screenRect.size;
///
/// 当前主页
///
/// The main page.
public static Shared.FrameLayout MainPage
{
get;
private set;
}
static int mainThreadId;
static UIView rootFrameLayout;
///
/// 根容器视图
///
/// The root frame layout.
public static UIView RootFrameLayout
{
get
{
return rootFrameLayout;
}
set
{
mainThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
rootFrameLayout = value;
CurrentWidth = (int)value.Frame.Width;
Shared.HDLUtils.WriteLine ("3========" + CurrentWidth);
CurrentHeight = (int)value.Frame.Height;
MainPage = new FrameLayout(value) { Width = (int)value.Frame.Width, Height = (int)value.Frame.Height, Parent = new RootView() { Width = (int)value.Frame.Width, Height = (int)value.Frame.Height } };
MainPage.Refresh();
}
}
///
/// 数据文件根目录
///
/// The root path.
public static string RootPath=System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)+"/";
///
/// 图片的根目录
///
/// The root path image.
public static string RootPathImage
{
get
{
return RootPath + "Phone/";
}
}
///
/// 是否是pad
///
public static bool IsPad {
get {
return false;
//return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad;
}
}
///
/// 皮肤,null时表示用默认的
///
public static string Skin;
///
/// GPS位置变化事件
///
public static Action LocationAction;
//获取当前屏幕显示的viewcontroller
public static UIViewController currentVC
{
get
{
var rootViewController = UIApplication.SharedApplication.KeyWindow.RootViewController;
var currentVC = getCurrentVCFrom(rootViewController);
return currentVC;
}
}
static UIViewController getCurrentVCFrom(UIViewController rootVC)
{
UIViewController currentVC;
if (rootVC.PresentedViewController != null)
{
// 视图是被presented出来的
rootVC = rootVC.PresentedViewController;
}
if (rootVC.GetType() == typeof(UITabBarController))
{
// 根视图为UITabBarController
currentVC = getCurrentVCFrom((rootVC as UITabBarController).SelectedViewController);
}
else if (rootVC.GetType() == typeof(UINavigationController))
{
// 根视图为UINavigationController
currentVC = getCurrentVCFrom((rootVC as UINavigationController).VisibleViewController);
}
else
{
// 根视图为非导航类
currentVC = rootVC;
}
return currentVC;
}
///
/// 开启定位服务
///
public static void StartGPSLocationService()
{
if (MyCLLocationManager.Instance.IsLocationServicesEnabled)
{
switch (Shared.Application.CurrentGpsUseMode)
{
case Application.GpsUseMode.Always:
MyCLLocationManager.Instance.RequestAlwaysAuthorization();
break;
case Application.GpsUseMode.WhenInUse:
MyCLLocationManager.Instance.RequestWhenInUseAuthorization();
break;
}
MyCLLocationManager.Instance.StartUpdatingLocation();
//if (launchOptions != null && launchOptions.ObjectForKey(UIApplication.LaunchOptionsLocationKey) != null)
//{
// MyCLLocationManager.Instance.StartMonitoringSignificantLocationChanges();
//}
//else
//{
// MyCLLocationManager.Instance.StartUpdatingLocation();
//}
}
else
{
HDLUtils.WriteLine("没权限");
///TODO 可以提示用户打开定位权限
}
}
public static void CheckLocationPermission() {
Application.CheckLocationPermission();
}
///
/// 关闭定位服务
///
public static void StopGPSLocationService()
{
if (MyCLLocationManager.Instance.IsLocationServicesEnabled)
{
MyCLLocationManager.Instance.StopUpdatingLocation();
}
else
{
HDLUtils.WriteLine("没权限");
}
}
///
/// 设置顶部状态栏字体颜色
///
/// 是否设置黑色
public static void SetStatusBarTextBlack(bool isBlack)
{
if (isBlack)
{
UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.LightContent;
}
else
{
UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.Default;
}
}
}
}