WPF - 宽度和高度必须是非负的

时间:2010-11-29 02:56:30

标签: wpf

我的应用程序遇到了一个奇怪的异常。它一直运行良好,直到我升级我的开发机器(相同的OS + 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()

2 个答案:

答案 0 :(得分:7)

您需要检查宽度和高度。这是一个相当安全的赌注,其中一个或两个都是负面的。我不会说这是肯定的,但我愿意在它上面投入很多钱: - )

对于它的价值,这个可能是一个改变了显示器分辨率的问题。有些人会做一个简单的计算,从你的屏幕尺寸中减去你的窗口大小,除以2来居中它。

如果结果显示您的窗口大于屏幕,则无论是位置还是尺寸,您都可能获得负值。

在没有看到计算宽度和高度的代码的情况下,这当然是猜想。我建议发布和/或临时添加代码以某种方式输出它,这样你就可以看到发生了什么。

答案 1 :(得分:2)

我不想因此而受到赞扬,但是当他提出显示问题时,paxdiablo是完全正确的。如果我能评论支持他,我会,但需要50个代表。

这是我的问题:

我刚刚开始使用3台显示器;我的笔记本电脑和两台22英寸显示器。

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

* FIX *

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