Exemple #1
0
        /// <summary>
        /// Executes the specified action with the specified arguments synchronously on the thread the Dispatcher is associated with.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <param name="onlyInvokeWhenNoAccess">If set to <c>true</c>, the action will be executed directly if possible. Otherwise,
        /// <c>Dispatcher.BeginInvoke</c> will be used.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="action" /> is <c>null</c>.</exception>
        public virtual void Invoke(Action action, bool onlyInvokeWhenNoAccess = true)
        {
            Argument.IsNotNull("action", action);
#if XAMARIN_FORMS
            var synchronizationContext = SynchronizationContext.Current;
            if (synchronizationContext != null)
            {
                synchronizationContext.Post(state => action(), null);
            }
            else
            {
                action();
            }
#elif ANDROID
            _handler.Post(action);
#elif IOS
            DispatchQueue.MainQueue.DispatchSync(() => action());
#else
            var dispatcher = CurrentDispatcher;
            DispatcherExtensions.Invoke(dispatcher, action, onlyInvokeWhenNoAccess);
#endif
        }