dotnet/maui · error · InvalidOperationException

StackDepth is not supported globally on Windows, please use

Error message

StackDepth is not supported globally on Windows, please use a LightNavigationPage.

What it means

IFormsNavigation.StackDepth is an expression-bodied property whose getter throws InvalidOperationException stating StackDepth is not supported globally on Windows and instructs to use a LightNavigationPage. Reading StackDepth on the FormsWindow navigation proxy is unsupported; only a LightNavigationPage exposes a meaningful depth.

Source

Thrown at src/Compatibility/Core/src/WPF/Interfaces/IFormsNavigation.cs:111

		public void PushModal(object page)
		{
			PushModal(page, true);
		}

		public void PushModal(object page, bool animated)
		{
			ParentWindow?.PushModal(page, animated);
		}

		public void RemovePage(object page)
		{
			throw new InvalidOperationException(
				"RemovePage is not supported globally on Windows, please use a LightNavigationPage.");
		}

		public int StackDepth =>
			throw new InvalidOperationException(
				"StackDepth is not supported globally on Windows, please use a LightNavigationPage.");
	}
}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Wrap pages in a LightNavigationPage and read its StackDepth.
  2. Branch by platform or by type: read depth only when Navigation is a per-page NavigationPage.
  3. Use the NavigationPage's NavigationStack.Count rather than the global proxy.
  4. Guard the read with a type check (is LightNavigationPage) and fall back to 0.

Example fix

// before
var depth = Navigation.StackDepth;

// after
var depth = Navigation is LightNavigationPage lnp ? lnp.StackDepth : 0;
Defensive patterns

Strategy: type-guard

Validate before calling

int depth = Navigation is LightNavigationPage lnp ? lnp.StackDepth : 0;

Type guard

static bool HasStackDepth(NavigationProxy n) => n is LightNavigationPage;

Try / catch

int depth;
try { depth = Navigation.StackDepth; }
catch (InvalidOperationException ex) when (ex.Message.Contains("LightNavigationPage"))
{ depth = 0; }

Prevention

When it happens

Trigger: Reading Navigation.NavigationStack.Count or StackDepth against the window-level navigation proxy; binding/logging code that accesses StackDepth unconditionally.

Common situations: Cross-platform code querying stack depth for back-button enable/disable; analytics/logging reading depth on all platforms.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/c5f580170d2cb8cd. Report an issue: GitHub.