dotnet/wpf · error · InvalidOperationException
SR.Rect_CannotCallMethod
Error message
SR.Rect_CannotCallMethod
What it means
Rect.Offset(Vector) throws InvalidOperationException when the rect is Empty. There is no defined position to offset for the NaN-based empty sentinel, and Offset would overwrite X/Y with NaN-derived values, so WPF declares the operation illegal on Empty (the XML docs state 'If this is Empty, this method is illegal').
Solutions
- Guard with if (!rect.IsEmpty) before calling Offset.
- If the rect should exist, initialize it with a real Rect(x, y, w, h) instead of Rect.Empty/default.
- Assign a new rect at the target position: rect = new Rect(newPoint, rect.Size) once non-empty.
Example fix
// before
viewport.Offset(panVector); // InvalidOperationException when viewport is Empty
// after
if (!viewport.IsEmpty) { viewport.Offset(panVector); } else { viewport = new Rect(panVector, initialSize); } Defensive patterns
Strategy: validation
Validate before calling
if (!rect.IsEmpty) { rect.Offset(offsetVector); } Type guard
static bool CanOffset(Rect r) => !r.IsEmpty;
Try / catch
try { rect.Offset(v); }
catch (InvalidOperationException) { rect = new Rect((Point)((Vector)v), new Size(0, 0)); } Prevention
- Always check IsEmpty before Offset/IntersectWith-style mutations.
- If accumulating bounds, use Rect.Union which tolerates Empty operands.
- Initialize viewport/selection rects with real geometry before panning logic runs.
When it happens
Trigger: Calling rect.Offset(vector) or rect.Offset(dx, dy) when rect == Rect.Empty — e.g. panning/moving a rect that came from an empty hit-test or intersection result.
Common situations: Viewport panning code moving a selection rect that was never set; animation updating offset each frame while the rect starts as Empty; chaining Offset after Intersect without checking overlap.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- SR.Rect_CannotModifyEmptyRect
- Current DocumentSequence, FixedDocument, or FixedPage not…
- IAmbientProvider
- InvalidOperationException()
- IXamlSchemaContextProvider
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/e06b2a6850d3774c.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Rect.cs:582
/// <summary>
/// Union - Return the result of the union of rect and point.
/// </summary>
public static Rect Union(Rect rect, Point point)
{
rect.Union(new Rect(point, point));
return rect;
}
/// <summary>
/// Offset - translate the Location by the offset provided.
/// If this is Empty, this method is illegal.
/// </summary>
public void Offset(Vector offsetVector)
{
if (IsEmpty)
{
throw new System.InvalidOperationException(SR.Rect_CannotCallMethod);
}
_x += offsetVector._x;
_y += offsetVector._y;
}
/// <summary>
/// Offset - translate the Location by the offset provided
/// If this is Empty, this method is illegal.
/// </summary>
public void Offset(double offsetX, double offsetY)
{
if (IsEmpty)
{
throw new System.InvalidOperationException(SR.Rect_CannotCallMethod);
}
_x += offsetX;View on GitHub (pinned to 81131a70a4)