dotnet/wpf · error · ArgumentException

SR.AudioVideo_InvalidDependencyObject

Error message

SR.AudioVideo_InvalidDependencyObject

What it means

AVElementHelper.GetHelper throws this ArgumentException when the passed DependencyObject is not a MediaElement (the lookup of the associated MediaElement helper fails). Only media-capable elements (MediaElement) can be used with AVElementHelper.

Solutions

  1. Check that the element is a MediaElement before calling AVElementHelper APIs (e.g. `if (obj is MediaElement me) ...`).
  2. Pass the MediaElement instance itself, not its parent or a container.
  3. Use VisualTreeHelper searches that filter to MediaElement if the element is located via the tree.

Example fix

// before
var helper = AVElementHelper.OnCreate(someElement);
// after
if (someElement is MediaElement media)
    var helper = AVElementHelper.OnCreate(media);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(depObj is MediaElement)) throw new ArgumentException("AVElementHelper requires a MediaElement");

Type guard

bool IsMediaElement(DependencyObject o) => o is MediaElement;

Try / catch

try { var el = AVElementHelper.OnCreate(obj); } catch (ArgumentException) { /* obj is not a MediaElement */ }

Prevention

When it happens

Trigger: Calling AVElementHelper.OnCreate/GetHelper-style APIs with a DependencyObject that is not a MediaElement (e.g. an Image, Button, or a custom element).

Common situations: Passing the wrong element to media helper APIs in code-behind; wiring AVElementHelper in a generic control container loop where the child may not be a MediaElement.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/AVElementHelper.cs:99

        #endregion

        #region Internal and Private Properties / Methods

        /// <summary>
        /// Returns the helper class given a dependency object
        /// </summary>
        internal static AVElementHelper GetHelper(DependencyObject d)
        {
            MediaElement mediaElement = d as MediaElement;

            if (mediaElement != null)
            {
                return mediaElement.Helper;
            }
            else
            {
                throw new ArgumentException(SR.AudioVideo_InvalidDependencyObject);
            }
        }

        /// <summary>
        /// MediaPlayer associated with the element.
        /// </summary>
        internal MediaPlayer Player
        {
            get
            {
                return _mediaPlayer;
            }
        }

        /// <summary>
        /// Base Uri to use when resolving relative Uri's
        /// </summary>
        internal Uri BaseUri

View on GitHub (pinned to 81131a70a4)