HandyOrg/HandyControl · error · ArgumentNullException
不能为 null
Error message
{nameof(window)} 不能为 null What it means
Validation guard at the entry of StartFullScreen: the window argument is null. The helper stores and mutates window state (styles, DWM transitions, monitor metrics) via Win32 interop, so a null reference must be rejected before any DependencyProperty or handle access. The message string is built with nameof(window) — 'window 不能为 null' means 'window cannot be null'.
Solutions
- Pass a live, initialized Window instance to StartFullScreen/EndFullScreen — construct the window and ensure it is loaded before going full screen
- Guard at the call site: 'if (window == null) return;' or log and skip when the window reference may be missing (e.g. during shutdown)
- For APIs that can legitimately receive null, make the parameter nullable and treat null as a no-op instead of throwing
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at src/Shared/HandyControl_Shared/Tools/Helper/FullScreenHelper.cs:41 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of HandyOrg/HandyControl@2c0875ebd6 (2026-09-14).
Data as JSON: /api/errors/076707a1c38421f7.
Report an issue: GitHub.
Appendix: source
Thrown at src/Shared/HandyControl_Shared/Tools/Helper/FullScreenHelper.cs:41
/// <summary>
/// 用于记录窗口全屏前样式的附加属性
/// </summary>
private static readonly DependencyProperty BeforeFullScreenWindowStyleProperty =
DependencyProperty.RegisterAttached("BeforeFullScreenWindowStyle",
typeof(InteropValues.WindowStyles?), typeof(FullScreenHelper));
/// <summary>
/// 开始进入全屏模式
/// 进入全屏模式后,窗口可通过 API 方式(也可以用 Win + Shift + Left/Right)移动,调整大小,但会根据目标矩形寻找显示器重新调整到全屏状态。
/// 进入全屏后,不要修改样式等窗口属性,在退出时,会恢复到进入前的状态
/// 进入全屏模式后会禁用 DWM 过渡动画
/// </summary>
public static void StartFullScreen(System.Windows.Window window)
{
if (window == null)
{
throw new ArgumentNullException(nameof(window), $"{nameof(window)} 不能为 null");
}
//确保不在全屏模式
if (window.GetValue(BeforeFullScreenWindowPlacementProperty) == null &&
window.GetValue(BeforeFullScreenWindowStyleProperty) == null)
{
var hwnd = new WindowInteropHelper(window).EnsureHandle();
var hwndSource = HwndSource.FromHwnd(hwnd);
//获取当前窗口的位置大小状态并保存
var placement = InteropMethods.GetWindowPlacement(hwnd);
window.SetValue(BeforeFullScreenWindowPlacementProperty, placement);
//修改窗口样式
var style = (InteropValues.WindowStyles) InteropMethods.GetWindowLongPtr(hwnd, InteropValues.GWL_STYLE);
window.SetValue(BeforeFullScreenWindowStyleProperty, style);
//将窗口恢复到还原模式,在有标题栏的情况下最大化模式下无法全屏,
//这里采用还原,不修改标题栏的方式View on GitHub (pinned to 2c0875ebd6)