public static async Task InvokeOnMainThreadAsync(this UIApplication sharedApplication, Func <Task> asyncDelegateToExecute) { var tcs = new TaskCompletionSource <Unit>(); sharedApplication.InvokeOnMainThread(async() => { try { await asyncDelegateToExecute(); tcs.TrySetResult(Unit.Default); } catch (Exception ex) { tcs.TrySetException(ex); } }); await tcs.Task.ConfigureAwait(false); }
/// <summary> /// Reset Application Icon Badge Number when there are no notifications. /// </summary> /// <param name="uiApplication"></param> public static void ResetApplicationIconBadgeNumber(UIApplication uiApplication) { if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0) == false) { return; } //Remove badges on app enter foreground if user cleared the notification in the notification panel UNUserNotificationCenter.Current.GetDeliveredNotifications((notificationList) => { if (notificationList.Any()) { return; } uiApplication.InvokeOnMainThread(() => { uiApplication.ApplicationIconBadgeNumber = 0; }); }); }
public static async Task <T> InvokeOnMainThreadAsync <T>(this UIApplication sharedApplication, Func <Task <T> > asyncDelegateToExecute) { var tcs = new TaskCompletionSource <T>(); sharedApplication.InvokeOnMainThread(async() => { try { var result = await asyncDelegateToExecute(); tcs.TrySetResult(result); } catch (Exception ex) { tcs.TrySetException(ex); } }); return(await tcs.Task.ConfigureAwait(false)); }
public static void SafeInvokeOnMainThread(this UIApplication app, Action action) => app.InvokeOnMainThread(() => { try { action(); } catch (Exception ex) { // Log.Error("", ex.ToString()); } });