WPF - width and height must be non-negative

I am experiencing a strange exception from my application. It worked well until I updated my development machine (same OS + VS2010) and tried to debug the software again. This exception I get:

Width and height must be non-negative.

The stack trace is here:

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() 
+6
wpf
source share
2 answers

You need to check your width and height. This is a fairly safe bet than one or both of them are negative. I will not say that it is definitely, but I am ready to bet on this a lot :-)

For what it's worth, it could be a problem with the changed resolution of the monitor. Some people will do a simplified calculation by subtracting the size of your window from your screen size and dividing it in two to center it.

If it turns out that your window is larger than your screen, you are likely to get negative values ​​for any position or size.

Without looking at code that calculates width and height, this, of course, is all guesswork. I suggest posting this and / or temporarily adding code to output it anyway so you can see what is happening.

+7
source share

I don't want to take it for granted, but paxdiablo is absolutely right when he suggested a display problem. If I could comment on his support, I would do it, but 50 responses were needed.

Here is my problem:

I recently started using 3 monitors; my laptop and two 22-inch monitors.

To make it work, I had to move the monitors on the screen resolution page until I had the correct order. However, I accidentally moved one monitor above the others. Visual Studio will fail on a monitor that does not match the other two.

* FIX *

  • Right click on the desktop
  • Choose screen resolution
  • Be sure to move the monitor screens so that they are all aligned to the top.
  • Apply and click "OK."
+1
source share

All Articles