unoplatform/uno · error · InvalidOperationException

MediaPlayerPresenterExtension must be initialized with a Med

Error message

MediaPlayerPresenterExtension must be initialized with a MediaPlayer instance

What it means

MediaPlayerPresenterExtension (WASM) implements IMediaPlayerPresenterExtension and its constructor requires the owner object passed by the framework to be exactly a MediaPlayerPresenter. Uno's extensibility layer instantiates presenter extensions by passing the host control as `object owner`; if anything other than MediaPlayerPresenter is supplied the cast guard throws InvalidOperationException. This indicates a wiring/registration mistake: the extension was registered for the wrong host type, or a custom presenter fed in a foreign object.

Source

Thrown at src/AddIns/Uno.UI.MediaPlayer.WebAssembly/MediaPlayerPresenterExtension.cs:29

using Microsoft.UI.Xaml.Media;

[assembly: ApiExtension(typeof(IMediaPlayerPresenterExtension), typeof(Uno.UI.Media.MediaPlayerPresenterExtension))]

namespace Uno.UI.Media;

public class MediaPlayerPresenterExtension : IMediaPlayerPresenterExtension
{
	private MediaPlayerPresenter? _owner;
	private HtmlMediaPlayer _htmlPlayer;

	public uint NaturalVideoHeight => (uint)_htmlPlayer.VideoHeight;
	public uint NaturalVideoWidth => (uint)_htmlPlayer.VideoWidth;

	public MediaPlayerPresenterExtension(object owner)
	{
		if (owner is not MediaPlayerPresenter presenter)
		{
			throw new InvalidOperationException($"MediaPlayerPresenterExtension must be initialized with a MediaPlayer instance");
		}

		_owner = presenter;
		_owner.Child = _htmlPlayer = new HtmlMediaPlayer();
	}

	public void MediaPlayerChanged()
	{
		if (this.Log().IsEnabled(LogLevel.Debug))
		{
			this.Log().LogDebug("Enter MediaPlayerPresenterExtension.MediaPlayerChanged().");
		}
		if (_owner is not null
			&& MediaPlayerExtension.GetByMediaPlayer(_owner.MediaPlayer) is { } extension)
		{
			extension.HtmlPlayer = _htmlPlayer;
		}
		else

View on GitHub (pinned to 0418340488)

Solutions

  1. Verify the [assembly: ApiExtension(...)] registration points the presenter extension at MediaPlayerPresenter (the framework-supplied owner).
  2. If you instantiated the extension manually, pass a MediaPlayerPresenter instance, not a generic FrameworkElement.
  3. Ensure only one media presenter extension assembly is loaded for the target so the framework resolves the correct type.

Example fix

// before (wrong owner)
var ext = new MediaPlayerPresenterExtension(someGrid);

// after
var ext = new MediaPlayerPresenterExtension(myMediaPlayerPresenter);
Defensive patterns

Strategy: validation

Validate before calling

if (owner is not MediaPlayerPresenter) throw new ArgumentException(nameof(owner));
var ext = new MediaPlayerPresenterExtension(owner);

Type guard

static bool IsValidOwner(object owner) => owner is MediaPlayerPresenter;

Prevention

When it happens

Trigger: The extension is constructed with an owner that is not MediaPlayerPresenter — typically because of a misconfigured ApiExtension registration, a custom MediaPlayerPresenter subclass whose runtime type trips an `is` pattern edge case, or a manual instantiation that passed the wrong control.

Common situations: Forking/extending the media stack with a custom presenter and registering MediaPlayerPresenterExtension against the wrong host; calling `new MediaPlayerPresenterExtension(someControl)` directly in tests; an assembly-load order issue resolving the wrong extension type.

Related errors


AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13). Data as JSON: /api/errors/f51ee8cebc4d53b7. Report an issue: GitHub.