WPF - 宽度和高度必须为非负数

7

我的应用程序出现了一个奇怪的异常。在我升级开发机器(相同的操作系统+VS2010)并尝试再次调试软件之前,它一直工作得很好。这是我得到的异常:

宽度和高度必须为非负数。

堆栈跟踪在此处:

 at System.Windows.Size..ctor(Double width, Double height)
   at System.Windows.Window.SourceWindowHelper.GetHwndNonClientAreaSizeInMeasureUnits()
   at System.Windows.Window.GetHwndNonClientAreaSizeInMeasureUnits()
   at System.Windows.Window.MeasureOverrideHelper(Size constraint)
   at System.Windows.Window.MeasureOverride(Size availableSize)
   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
   at System.Windows.UIElement.Measure(Size availableSize)
   at System.Windows.ContextLayoutManager.UpdateLayout()
   at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
   at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run(Window window)
   at System.Windows.Application.Run()
   at AdvancedConsole2._0.App.Main() in F:\Projects\hitech\AdvancedConsole2.0\trunk\AdvancedConsole2.0\AdvancedConsole2.0\obj\x86\Debug\App.g.cs:line 0
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

你的宽度和高度数值是多少?调试器能够显示它们吗? - chrisaycock
3个回答

7
你需要检查你的宽度和高度。很有可能其中一个或两个是负数。虽然我不敢说这是一定的,但我愿意打赌很多钱 :-)
如果你改变了显示器分辨率,这可能会成为一个问题。有些人会做一个简单的计算,将你的窗口大小从屏幕大小中减去并除以二来居中。
如果你的窗口比屏幕大,你可能会得到负值,无论是位置还是大小。
当然,在没有看到计算宽度和高度的代码之前,这都只是猜测。我建议发布或暂时添加代码来输出它,以便你可以看到发生了什么。

第二个指针正好命中了目标。我有一个自定义控件,监听调整大小事件,结果是负数。+1 - Tri Q Tran

2

我不想抢夺功劳,但当paxdiablo建议这是一个显示问题时,他是完全正确的。如果我能评论支持他的话,我会的,但需要50个声望。

这是我的问题:

我最近开始使用3个显示器:我的笔记本电脑和两个22英寸的显示器。

为了让它工作,我必须在屏幕分辨率页面上移动显示器,直到我有正确的顺序。在这样做时,我意外地将一个显示器移动到比其他两个高。Visual Studio在与其他两个不对齐的显示器上崩溃。

* 修复方法 *

  1. 右键单击桌面
  2. 选择屏幕分辨率
  3. 确保移动显示器屏幕,使它们都对齐在顶部。
  4. 应用并单击“确定”。

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接