WPF仅代码应用程序GIVES“对象引用未设置为对象的实例。”

时间:2012-07-05 00:37:55

标签: c# wpf

我正在尝试创建一个仅代码的WPF应用程序,但是当我输入textBox时出现上述错误。虽然我的所有变量都已初始化。

windows1.xaml是这样的:

<?xml version="1.0" encoding="utf-8"?>

<Window>

    x:Class="BlendCatalogue.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="BlendCatalogue"
    Height="300"
    Width="300">
</Window>

背后的代码是:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace BlendCatalogue
{

    public partial class Window1 : Window
    {
            private TextBlock textBlock;
            private TextBox textBox;

        public Window1()
        {
            InitializeComponent();
            Initialization();
        }

        public void Initialization()
        {


            this.Width=300;
            this.Height=200;
            this.Background =Brushes.Aquamarine;
            this.Title = "Only the best!";

            Grid layoutGrid = new Grid();
            StackPanel stackpanel = new StackPanel();
            layoutGrid.Children.Add(stackpanel);
            this.AddChild(layoutGrid);

            TextBlock textBlock = new TextBlock();
            textBlock.Margin = new Thickness(6);
            textBlock.Height = 20;
            textBlock.TextAlignment = TextAlignment.Center;
            textBlock.Text = "Hello my World!";
            stackpanel.Children.Add(textBlock);


            TextBox textBox = new TextBox();
            textBox.Margin = new Thickness(5);
            textBox.Width = 150;
            textBox.TextAlignment = TextAlignment.Center;
            textBox.Text = "";
            textBox.TextChanged += OnTextChanged;
            stackpanel.Children.Add(textBox);

            Button btnColor = new Button();
            btnColor.Margin = new Thickness(5);
            btnColor.Width = 150;
            btnColor.Content = "Change Text Color";
            btnColor.Click += btnChangeColor_Click;
            stackpanel.Children.Add(btnColor);

            Button btnSize = new Button();
            btnSize.Margin = new Thickness(5);
            btnSize.Width = 150;
            btnSize.Content = "Change Text Color";
            btnSize.Click += btnChangeSize_Click;
            stackpanel.Children.Add(btnSize);
        }

        private void OnTextChanged(object sender, TextChangedEventArgs e)
        {

            textBlock.Text = textBox.Text;
        }

        private void btnChangeColor_Click(object sender, RoutedEventArgs e)
        {
            if (textBlock.Foreground == Brushes.Black)
                textBlock.Foreground = Brushes.Red;
            else
                textBlock.Foreground = Brushes.Black;
        }
        private void btnChangeSize_Click(object sender, RoutedEventArgs e)
        {
            if (textBlock.FontSize == 11)
                textBlock.FontSize = 42;
            else
                textBlock.FontSize = 11;
        }

    }
}

这个新手根本不知道做错了什么,真的很感激任何帮助。 谢谢大家。

2 个答案:

答案 0 :(得分:0)

您将两个变量声明两次:

private TextBlock textBlock;
private TextBox textBox;

TextBlock textBlock = new TextBlock();
TextBox textBox = new TextBox();

您将作用域Initialize()初始化,但访问事件处理程序中的类级别变量。

更改

TextBlock textBlock = new TextBlock();
TextBox textBox = new TextBox();

textBlock = new TextBlock();
textBox = new TextBox();

请注意,您应该收到像

这样的编译器警告
  

警告CS0649:字段'BlendCatalogue.Window1.textBlock'永远不会分配给,并且将始终具有其默认值null

编译器试图帮助你......: - )

答案 1 :(得分:0)

您正在创建两个TextBlock's和两个TextBox's一个具有模块级别范围,一个具有本地范围。然后,使用Local scope初始化一个,并尝试使用模块级作用域导致错误。

尝试将初始化方法中的代码更改为:

textBlock = new TextBlock();
textBlock.Margin = new Thickness(6);
textBlock.Height = 20;
textBlock.TextAlignment = TextAlignment.Center;
textBlock.Text = "Hello my World!";
stackpanel.Children.Add(textBlock);


textBox = new TextBox();
textBox.Margin = new Thickness(5);
textBox.Width = 150;
textBox.TextAlignment = TextAlignment.Center;
textBox.Text = "";
textBox.TextChanged += OnTextChanged;
stackpanel.Children.Add(textBox);