Exemplo n.º 1
0
        bool DoValidation(ExpressionValidationContext validationContext, out string errorMessage)
        {
            errorMessage = null;

            //validate
            //if the text is empty we clear the error message
            if (string.IsNullOrEmpty(validationContext.ExpressionText))
            {
                errorMessage = null;
                return(true);
            }
            // if the expression text is different from the last time we run the validation we run the validation
            else if (!string.Equals(validationContext.ExpressionText, validationContext.ValidatedExpressionText))
            {
                try
                {
                    //TODO: Add logic to validate expression
                }
                catch (Exception err)
                {
                    errorMessage = err.Message;
                }

                return(true);
            }

            return(false);
        }
Exemplo n.º 2
0
        void StartValidator()
        {
            if (Validator == null)
            {
                Validator = new BackgroundWorker();
                Validator.WorkerReportsProgress      = true;
                Validator.WorkerSupportsCancellation = true;

                Validator.DoWork += delegate(object obj, DoWorkEventArgs args)
                {
                    BackgroundWorker worker = obj as BackgroundWorker;
                    if (worker.CancellationPending)
                    {
                        args.Cancel = true;
                        return;
                    }
                    ExpressionValidationContext validationContext = args.Argument as ExpressionValidationContext;
                    if (validationContext != null)
                    {
                        string errorMessage;
                        if (DoValidation(validationContext, out errorMessage))
                        {
                            worker.ReportProgress(0, errorMessage);
                        }

                        //sleep
                        if (worker.CancellationPending)
                        {
                            args.Cancel = true;
                            return;
                        }

                        Thread.Sleep(ValidationWaitTime);
                        args.Result = validationContext;
                    }
                };

                Validator.RunWorkerCompleted += delegate(object obj, RunWorkerCompletedEventArgs args)
                {
                    if (!args.Cancelled)
                    {
                        ExpressionValidationContext validationContext = args.Result as ExpressionValidationContext;
                        if (validationContext != null)
                        {
                            Dispatcher.BeginInvoke(new Action <ExpressionValidationContext>((target) =>
                            {
                                //validator could be null by the time we try to validate again or
                                //if it's already busy
                                if (Validator != null && !Validator.IsBusy)
                                {
                                    target.Update(this);
                                    Validator.RunWorkerAsync(target);
                                }
                            }), validationContext);
                        }
                    }
                };

                Validator.ProgressChanged += delegate(object obj, ProgressChangedEventArgs args)
                {
                    string error = args.UserState as string;
                    Dispatcher.BeginInvoke(new Action <string>(UpdateValidationError), error);
                };

                Validator.RunWorkerAsync(new ExpressionValidationContext(this));
            }
        }