首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >意外NullReferenceException / InvocationTargetException

意外NullReferenceException / InvocationTargetException
EN

Stack Overflow用户
提问于 2014-03-28 07:53:56
回答 1查看 549关注 0票数 1

我指出了一个零错误,但我不知道问题出在哪里,因为在使用之前,所有的东西都是无效的。错误产生的点,我给出了一个区块引号。像往常一样感谢我的每一个帮助。

代码语言:javascript
复制
Button topAddBut = null;
//Button botAddBut = null;
Button loadBut = null;
Button saveBut = null;
StackPanel topSP = null;
//StackPanel botSP = null;      

public MainWindow()
{
      InitializeComponent();

      loadBut = new Button { Content = "Load", Width = 70, Height = 23 };
      Canvas.SetRight(loadBut, 160);
      Canvas.SetBottom(loadBut, 24);
      canvas1.Children.Add(loadBut);

      saveBut = new Button { Content = "Save", Width = 70, Height = 23 };
      Canvas.SetRight(saveBut, 80);
      Canvas.SetBottom(saveBut, 24);
      canvas1.Children.Add(saveBut);

      StackPanel topSP = new StackPanel { Width = 400, Height = 50 };
      Canvas.SetLeft(topSP, 160);
      Canvas.SetTop(topSP, 100);

      AddWrapPanelTop();

      AddTextBoxTop();
      AddTopButton();
}

void AddTextBoxTop()
{
     TextBox txtB1 = new TextBox();
     txtB1.Text = "Text";
     txtB1.Width = 75;
     txtB1.Height = 75;
     WrapPanel wp = (WrapPanel)topSP.Children[0];
     wp.Children.Add(txtB1);   
 }

 void AddWrapPanelTop()
 {    
      WrapPanel myWrapPanel = new WrapPanel();

      SolidColorBrush mySolidColorBrush = new SolidColorBrush();
      mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 255);

      myWrapPanel.Background = System.Windows.Media.Brushes.Magenta;
      myWrapPanel.Orientation = Orientation.Horizontal;
      myWrapPanel.Width = 4000;
      myWrapPanel.HorizontalAlignment = HorizontalAlignment.Left;
      myWrapPanel.VerticalAlignment = VerticalAlignment.Top;

      topSP.Children.Add(myWrapPanel);
  }

  void AddTopButton()
  {    
       TextBox txtB1 = new TextBox();
       txtB1.Background = System.Windows.Media.Brushes.Magenta;
       txtB1.Text = "Text";
       txtB1.Width = 75;
       txtB1.Height = 75;
       topAddBut = new Button();
       topAddBut.Click += new RoutedEventHandler(this.TopClick);
       topAddBut.Content = "Add";
       topAddBut.Width = 75;

       // Add the buttons to the parent WrapPanel using the Children.Add method.
       WrapPanel wp = (WrapPanel)topSP.Children[0];
       wp.Children.Add(txtB1);
       wp.Children.Add(loadBut);
       this.topSP.Children.Add(wp);
   }

   void TopClick(Object sender, EventArgs e)
   {
        TextBox txtB1 = new TextBox();
        txtB1.Background = System.Windows.Media.Brushes.Magenta;
        txtB1.Text = "Text";
        txtB1.Width = 75;
        txtB1.Height = 75;
        Button s = (Button)sender;
        WrapPanel wp = (WrapPanel)s.Parent;
        wp.Children.Remove(s);
        wp.Children.Add(txtB1);
        AddTopButton();

        // Add the buttons to the parent WrapPanel using the Children.Add method.
    }
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-28 08:00:46

定义如下:

代码语言:javascript
复制
StackPanel topSP = null;

那你就有

代码语言:javascript
复制
StackPanel topSP = new StackPanel { Width = 400, Height = 50 };

这是在您的本地范围内,尽管如此,当您退出该函数时,它将被销毁。

你终于有了

代码语言:javascript
复制
topSP.Children.Add(myWrapPanel);

这仍将被设置为null,这就是发生错误的原因。基本上,您正在创建相同变量的本地版本。

为了解决这个问题,只需将第二个定义更改为:

代码语言:javascript
复制
topSP = new StackPanel { Width = 400, Height = 50 };
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22707118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档