dotnet/wpf · error · InvalidOperationException

Operation cannot be performed.

Error message

Operation cannot be performed.

What it means

Invoke on a UI Automation hyperlink proxy throws ElementNotEnabledException if the window is disabled, and InvalidOperationException('Operation cannot be performed') if the hyperlink's underlying HWND is not visible. The library refuses to simulate a click on a hidden control because the operation cannot reach a visible target.

Solutions

  1. Ensure the hyperlink's window is visible before invoking (show the window, select the tab/page)
  2. Wait for the element to be visible (poll IsOffscreen or wait for window shown) before calling Invoke
  3. Catch InvalidOperationException and re-acquire the element after the UI is shown
  4. Use UIA caching/walkers to skip invisible elements

Example fix

// before
hyperlink.Invoke();
// after
if (!hyperlink.CurrentPropertyValue(AutomationElement.IsOffscreenProperty, true))
{
    hyperlink.Invoke();
}
else
{
    // show the parent window/tab first, then retry
}
Defensive patterns

Strategy: try-catch

Validate before calling

// before Invoke
bool visible = !((bool)hyperlink.GetCurrentPropertyValue(AutomationElement.IsOffscreenProperty));
if (!visible) { /* show window/tab or abort */ }

Try / catch

try { ((InvokePattern)hyperlink.GetCurrentPattern(InvokePattern.Pattern)).Invoke(); }
catch (InvalidOperationException) { /* element hidden/unavailable: show UI, re-acquire, retry */ }
catch (ElementNotEnabledException) { /* enable the element first */ }

Prevention

When it happens

Trigger: Calling IInvokeProvider.Invoke() on a hyperlink whose hosting window SafeNativeMethods.IsWindowVisible(_hwnd) returns false (e.g. parent window minimized, hyperlink on a hidden/inactive tab, element collapsed).

Common situations: UI Automation test scripts invoking links before showing the window; invoking links inside collapsed TabItems or hidden dialog pages; automation racing against window close/minimize.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/8c2550b06575ce01. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsHyperlink.cs:429

        #region Invoke Pattern

        // Same as clicking on an hyperlink
        void IInvokeProvider.Invoke()
        {
            // Check that button can be clicked.
            //
            // This state could change anytime.
            //

            // Make sure that the control is enabled
            if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
            {
                throw new ElementNotEnabledException();
            }

            if (!SafeNativeMethods.IsWindowVisible(_hwnd))
            {
                throw new InvalidOperationException(SR.OperationCannotBePerformed);
            }

            //
            // Get the bounding rect for the window.
            //
            NativeMethods.Win32Rect BoundingRect = NativeMethods.Win32Rect.Empty;
            if (!Misc.GetWindowRect(_hwnd, ref BoundingRect))
            {
                return;
            }

            //
            // All we really need here are the height and the width,
            // so we don't even need to translate between screen
            // and client coordinates.
            //
            int width = BoundingRect.right - BoundingRect.left;
            int height = BoundingRect.bottom - BoundingRect.top;

View on GitHub (pinned to 81131a70a4)