public async Task SetElement()

in src/AccessibilityInsights/Modes/TestModeControl.xaml.cs [127:281]


        public async Task SetElement(Guid ecId)
        {
            EnableMenuButtons(false); // first disable them to avoid any conflict.

            bool selectionFailure = false;

            if (GetDataAction.ExistElementContext(ecId))
            {
                EnableMenuButtons(this.DataContextMode != DataContextMode.Load);
                this.ctrlAutomatedChecks.Visibility = Visibility.Visible;
                this.instructionsAutomatedChecks.Visibility = Visibility.Collapsed;
                this.tabControl.IsEnabled = true;

                try
                {
                    AutomationProperties.SetName(this, Properties.Resources.TestModeControl_Test);

                    this.tabControl.IsEnabled = false;
                    this.ctrlProgressRing.Activate();

                    ElementContext ec = null;
                    string warning = string.Empty;

                    await Task.Run(() =>
                    {
                        bool contextChanged = CaptureAction.SetTestModeDataContext(ecId,
                            this.DataContextMode, Configuration.TreeViewMode);
                        ec = GetDataAction.GetElementContext(ecId);

                        // send telemetry of scan results.
                        var dc = GetDataAction.GetElementDataContext(ecId);
                        if (dc.ElementCounter.UpperBoundExceeded)
                        {
                            warning = string.Format(CultureInfo.InvariantCulture,
                                Properties.Resources.SetElementCultureInfoFormatMessage,
                                dc.ElementCounter.UpperBound);
                            IsSaveEnabled = false;
                        }
                        if (contextChanged && this.DataContextMode != DataContextMode.Load)
                        {
                            dc.PublishScanResults();
                        }
                    }).ConfigureAwait(false);

                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        if (ec.Element == null || ec.Element.Properties == null || ec.Element.Properties.Count == 0)
                        {
                            this.ctrlAutomatedChecks.ClearUI();
                            selectionFailure = true;
                        }
                        else
                        {
                            if (ec.DataContext.Screenshot == null && ec.DataContext.Mode != DataContextMode.Load)
                            {

                                if (ec.Element.BoundingRectangle.IsEmpty == true)
                                {
                                    MessageDialog.Show(Properties.Resources.noScreenShotAvailableMessage);
                                }
                                else
                                {
                                    Application.Current.MainWindow.WindowStyle = WindowStyle.ToolWindow;
                                    Application.Current.MainWindow.Visibility = Visibility.Hidden;

                                    HollowHighlightDriver.GetDefaultInstance().IsEnabled = false;

                                    this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() =>
                                    {
                                        ScreenShotAction.CaptureScreenShot(ecId);
                                        Application.Current.MainWindow.WindowStyle = WindowStyle.SingleBorderWindow;

                                        // This needs to happen before the call to ctrlAutomatedChecks.SetElement. Otherwise,
                                        // ctrlAutomatedChecks's checkboxes become out of sync with the highlighter
                                        Application.Current.MainWindow.Visibility = Visibility.Visible;
                                    })).Wait();
                                }
                            }
                            this.tabControl.IsEnabled = true;
                            this.ctrlAutomatedChecks.SetElement(ec);

                            this.ElementContext = ec;

                            if (ec.DataContext.Mode == DataContextMode.Test)
                            {
                                tbiTabStop.Visibility = Visibility.Visible;
                                this.ctrlTabStop.SetElement(ec);
                            }
                            else
                            {
                                tbiTabStop.Visibility = Visibility.Collapsed;
                            }

                            var count = ec.DataContext?.GetRuleResultsViewModelList()?.Count ?? 0;
                            AutomationProperties.SetName(this, 
                                string.Format(CultureInfo.InvariantCulture, Properties.Resources.TestModeControl_DetectedFailureFormat,
                                    this.ElementContext.Element.Glimpse, count));

                            if (!string.IsNullOrEmpty(warning))
                            {
                                MessageDialog.Show(warning);
                            }
                            ///Set focus on automated checks tab.
                            FireAsyncContentLoadedEvent(AsyncContentLoadedState.Completed);
                        }
                    });
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception e)
                {
                    e.ReportException();
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        Application.Current.MainWindow.Visibility = Visibility.Visible;
                        this.Clear();
                        selectionFailure = true;
                        EnableMenuButtons(false);
                        this.ElementContext = null;
                    });
                }
#pragma warning restore CA1031 // Do not catch general exception types
                finally
                {
                    Application.Current.Dispatcher.Invoke(() => {
                        this.ctrlProgressRing.Deactivate();
                    });
                }
            }

            Application.Current.Dispatcher.Invoke(() =>
            {
                // Set the right view state :
                if (selectionFailure)
                {
                    MainWin.HandleFailedSelectionReset();
                }
                else
                {
                    MainWin.SetCurrentViewAndUpdateUI(TestView.AutomatedTestResults);
                }

                // Improves the reliability of the rendering but does not solve across the board issues seen with all WPF apps.
                this.Dispatcher.BeginInvoke(DispatcherPriority.Send, new Action(() =>
                {
                    // This is a workaround for a repaint issue caused by an interaction between Jaws and WPF
                    if (NativeMethods.IsExternalScreenReaderActive())
                    {
                        Application.Current.MainWindow.Visibility = Visibility.Hidden;
                        Application.Current.MainWindow.InvalidateVisual();
                        Application.Current.MainWindow.Visibility = Visibility.Visible;
                    }
                    this.tbiAutomatedChecks.Focus();
                })).Wait();
            });
        }