0

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

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;
        }

    }
}

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

4

2 回答 2

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

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

于 2012-07-05T00:43:34.967 回答
0

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

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

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);
于 2012-07-05T00:52:59.280 回答