2010-11-29 269 views
6

我遇到了來自我的應用程序的奇怪異常。它一直運行良好,直到我升級了我的開發機器(相同的OS + VS2010)並試圖再次調試軟件。這是我得到的例外:WPF - 寬度和高度必須爲非負值

寬度和高度必須是非負值。

堆棧跟蹤這裏:

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() 
+0

你的寬度和高度值是多少?你可以讓調試器顯示它們嗎? – chrisaycock 2010-11-29 03:01:36

回答

8

你需要檢查你的寬度和高度。這是一個相當安全的賭注,而不是一個或兩個都是負面的。我不會說這是確定的,但我會願意賭上它很多錢:-)

對於什麼是值得,這可能是改變顯示器分辨率的問題。有些人會做一個簡單的計算,從屏幕尺寸中減去你的窗口大小,再除以2來居中。

如果事實證明你的窗口比你的屏幕大,你可能會得到負值,無論是位置還是大小。

沒有看到計算寬度和高度的代碼,這當然是全部猜想。我建議發佈和/或臨時添加代碼以便以某種方式輸出它,以便您可以看到發生了什麼。

+0

第二個指針擊中了現場。我有一個自定義控件,用於偵聽調整大小事件,結果是否定的。 +1 – 2010-11-30 02:47:10

1

我不想承認這一點,但paxdiablo是絕對正確的,當他提出顯示問題。如果我可以評論支持他,我會,但需要50代表。

這是我的問題:

我剛剛開始使用3臺顯示器;我的筆記本電腦和兩臺22英寸顯示器。

爲了讓它正常工作,我必須在屏幕分辨率頁面中移動顯示器,直到我有正確的順序。這樣做時,我意外地將一臺顯示器移到了其他顯示器上。監視器上的Visual Studio崩潰與其他兩個不一致。

* FIX *

  1. 右鍵點擊桌面上
  2. 選擇屏幕分辨率
  3. 確保移動顯示器屏幕使所有的人都在 頂部對齊。
  4. 應用並單擊確定。
相關問題