WindowState:System.InvalidOperationException:Nullable对象的值必须为

时间:2017-11-17 07:51:50

标签: c#

我正在开发一个Windows应用程序(在Windows 10上运行,使用VS2017构建),它引发了以下异常:

导致异常的方法是:

    private void RestoreWindow()
    {
        this.WindowState = WindowState.Normal;

        this.Width = this.LastSize.Value.Width;
        this.Height = this.LastSize.Value.Height;
        this.Left = this.LastSize.Value.X;
        this.Top = this.LastSize.Value.Y;

        this.LastSize = null;
    }

状态更改事件的处理方式如下:

    private void windowChat_StateChanged(object sender, EventArgs e)
    {
        if (WindowState.Normal == this.WindowState)
        {
            this.MaxHeight = Double.PositiveInfinity;
            this.MaxWidth = Double.PositiveInfinity;
        }
    }

我的理解是,当对象为NULL时会引发此异常,在本例中为WindowState,但我不知道这是怎么可能的,因为它是系统属性。

堆栈跟踪是:

  

System.InvalidOperationException:Nullable对象必须具有值   在System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource资源)
  在x.cs中的RestoreWindow():第324行   在x.cs中的RestoreButton_Click(Object sender,RoutedEventArgs e):第100行   在System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象目标,RoutedEventArgs routedEventArgs)
  在System.Windows.EventRoute.InvokeHandlersImpl(对象源,RoutedEventArgs args,布尔值reRaised)
  在System.Windows.UIElement.RaiseEventImpl(DependencyObject sender,RoutedEventArgs args)
  在System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
  在System.Windows.Controls.Primitives.ButtonBase.OnClick()
  在System.Windows.Controls.Button.OnClick()
  在System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
  在System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender,MouseButtonEventArgs e)
  在System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler,Object genericTarget)
  在System.Windows.RoutedEventArgs.InvokeHandler(委托处理程序,对象目标)
  在System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象目标,RoutedEventArgs routedEventArgs)
  在System.Windows.EventRoute.InvokeHandlersImpl(对象源,RoutedEventArgs args,布尔值reRaised)
  在System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender,RoutedEventArgs args,RoutedEvent newEvent)
  在System.Windows.UIElement.OnMouseUpThunk(对象发送者,MouseButtonEventArgs e)
  在System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler,Object genericTarget)
  在System.Windows.RoutedEventArgs.InvokeHandler(委托处理程序,对象目标)
  在System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象目标,RoutedEventArgs routedEventArgs)
  在System.Windows.EventRoute.InvokeHandlersImpl(对象源,RoutedEventArgs args,布尔值reRaised)
  在System.Windows.UIElement.RaiseEventImpl(DependencyObject sender,RoutedEventArgs args)
  在System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
  在System.Windows.UIElement.RaiseEvent(RoutedEventArgs args,布尔值信任)
  在System.Windows.Input.InputManager.ProcessStagingArea()
  在System.Windows.Input.InputManager.ProcessInput(InputEventArgs输入)
  在System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
  在System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd,InputMode模式,Int32时间戳,RawMouseActions操作,Int32 x,Int32 y,Int32轮)
  在System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd,WindowMessage msg,IntPtr wParam,IntPtr lParam,Boolean& processed)
  在System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean& handling)
  在MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean& handling)
  在MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
  在System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象args,Int32 numArgs)
  at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source,Delegate callback,Object args,Int32 numArgs,Delegate catchHandler)

1 个答案:

答案 0 :(得分:0)

堆栈跟踪在第324行指出了一个问题,就是这行代码:

this.WindowState = WindowState.Normal;

但问题是LastSize为空。