Exemplo n.º 1
0
        private static bool CanExecute(ActionId actionId)
        {
            if (!Enabled)
            {
                return(true);
            }

            foreach (ActionFlags flag in canExecuteFlags)
            {
                IAction action = GetAction(actionId);
                var     f      = flag & action.Flags;

                if (canExecuteCache.ContainsKey(f))
                {
                    return(canExecuteCache[f]);
                }

                IActionValidator validator    = GetValidator(f);
                string           error        = validator.Validate();
                bool             isCanExecute = string.IsNullOrWhiteSpace(error);
                canExecuteCache.Add(f, isCanExecute);

                if (!isCanExecute)
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        public static void Execute(ActionId actionId)
        {
#if DEBUG
            Log.Logger.DebugFormat("Action Execute = {0}", actionId);
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();
#endif
            try
            {
                IAction action = GetAction(actionId);

                foreach (ActionFlags flag in executeFlags)
                {
                    var f = flag & action.Flags;
                    IActionValidator validator = GetValidator(f);
                    string           error     = validator.Validate();

                    if (!string.IsNullOrWhiteSpace(error))
                    {
                        MessageDialog.Warn(error);
                        return;
                    }
                }

                action.Execute();
#if DEBUG
                sw.Stop();
                Log.Logger.DebugFormat("Action Completed = {0}, {1}", actionId, sw.Elapsed);
#endif
            }
            catch (AggregateException ex)
            {
                ex.Flatten();

                foreach (var e in ex.InnerExceptions)
                {
                    Log.Logger.Warn(e);
                }

                MessageDialog.Error(ex.InnerException.Message);
            }
            catch (InvalidOperationException ex)
            {
                Log.Logger.Warn(ex);
                MessageDialog.Warn(ex.Message);
            }
            catch (Exception ex)
            {
                Log.Logger.Error(ex);
                MessageDialog.Error(ex.Message);
            }
        }