locked
System.NullReferenceException RRS feed

  • Question

  • I create a userControl which override the touchUp event. Based on solution, I add 

    Window win = Window.GetWindow(this); win.TouchUp += (send, ee) => { MyTouchUpHandler(send, ee); };

    Into the my user control in order to react for whole screen event. But after I add this in my xaml file, cannot load the designer, and error message 

    System.NullReferenceException
    Object reference not set to an instance of an object.
       at serverControl.UserControl1.UserControl1_Loaded(Object sender, RoutedEventArgs e) in C:\Users\zhehuang\OneDrive for Business 2\2015Thiese File\serverControl_v7\serverControl\UserControl1.xaml.cs:line 88
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
       at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
       at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)
       at MS.Internal.LoadedOrUnloadedOperation.DoWork()
       at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
       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)
    

    The line 88 is 

                win.TouchUp += (send, ee) =>
                {
                    MyTouchUpHandler(send, ee);
                };

    I don't know anywhere is problem.

    Wednesday, February 25, 2015 3:05 PM

Answers

  • Looks like the Window.GetWindow() method always returns a null at design time (see here)

    Solution for you would appear to be the same given on the above link. i.e. test whether you are in design mode or not.

    if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) 
    {
       return;
    }

    • Proposed as answer by Magnus (MM8)MVP Wednesday, February 25, 2015 3:18 PM
    • Marked as answer by mozart0000 Wednesday, February 25, 2015 3:30 PM
    Wednesday, February 25, 2015 3:12 PM
  • Edit? Just try to return from the constructor and the Loaded event when you are in design mode:


    public partial class UserControl1 : UserControl
      {
        public UserControl1() {
          InitializeComponent();
          if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) 
          {
           return;
          }
          this.Loaded += UserControl1_Loaded;
        }
    
        void UserControl1_Loaded(object sender, RoutedEventArgs e) {
          if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) 
          {
           return;
          }
    
          Window win = Window.GetWindow(this);
    
          win.TouchUp += (ss, ee) =>
          {
            MyTouchUpHandler(ss, ee);
          };
        }
    
        protected override void OnTouchUp(TouchEventArgs e) {
          base.OnTouchUp(e);
    
          MyTouchUpHandler(this, e);
          e.Handled = true;
        }
    
        private void MyTouchUpHandler(object sender, TouchEventArgs e) {
          //do your thing here....
          
        }
      }
    

     

     

    Please remember to close your threads by marking all helpful posts as answer.

    • Marked as answer by mozart0000 Wednesday, February 25, 2015 3:30 PM
    Wednesday, February 25, 2015 3:29 PM

All replies

  • Looks like the Window.GetWindow() method always returns a null at design time (see here)

    Solution for you would appear to be the same given on the above link. i.e. test whether you are in design mode or not.

    if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) 
    {
       return;
    }

    • Proposed as answer by Magnus (MM8)MVP Wednesday, February 25, 2015 3:18 PM
    • Marked as answer by mozart0000 Wednesday, February 25, 2015 3:30 PM
    Wednesday, February 25, 2015 3:12 PM
  • How can I edit with that code?
    Wednesday, February 25, 2015 3:25 PM
  • Edit? Just try to return from the constructor and the Loaded event when you are in design mode:


    public partial class UserControl1 : UserControl
      {
        public UserControl1() {
          InitializeComponent();
          if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) 
          {
           return;
          }
          this.Loaded += UserControl1_Loaded;
        }
    
        void UserControl1_Loaded(object sender, RoutedEventArgs e) {
          if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) 
          {
           return;
          }
    
          Window win = Window.GetWindow(this);
    
          win.TouchUp += (ss, ee) =>
          {
            MyTouchUpHandler(ss, ee);
          };
        }
    
        protected override void OnTouchUp(TouchEventArgs e) {
          base.OnTouchUp(e);
    
          MyTouchUpHandler(this, e);
          e.Handled = true;
        }
    
        private void MyTouchUpHandler(object sender, TouchEventArgs e) {
          //do your thing here....
          
        }
      }
    

     

     

    Please remember to close your threads by marking all helpful posts as answer.

    • Marked as answer by mozart0000 Wednesday, February 25, 2015 3:30 PM
    Wednesday, February 25, 2015 3:29 PM