我正在制作 C# WPF 程式。我正在嘗試撰寫代碼,每當 TextBox 的背景關系發生更改時,都會更改 TextString(它是 TextBlock)。但是每當我構建我的程式時,它就會發生!“System.NullReferenceException:'物件參考未設定為物件的實體。'”請幫幫我:D
<Window x:Class="TextInputOutput.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TextInputOutput"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="858">
<Grid>
<Button Content="????" HorizontalAlignment="Left" Margin="346,243,0,0" VerticalAlignment="Top" Height="35" Width="70"/>
<Border BorderThickness="1" BorderBrush="Black"/>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Margin="296,161,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged"/>
<TextBlock x:Name="TextString" HorizontalAlignment="Left" Margin="459,163,0,0" Text="Dd" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TextInputOutput
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
TextString.Text = "Hello Buddy!";
this.textBox.TextChanged = TextBox_TextChanged;
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextString.Text = textBox.Text;
}
}
}
uj5u.com熱心網友回復:
問題是,您在創建文本塊之前創建了文本框。因為您在 XAML 代碼中設定了文本,所以將在創建文本塊之前引發 Textchanged 事件,然后您的錯誤訊息會出現文本塊為空。所以你必須做一個 if 陳述句并檢查你的文本框是否為空,如果它不為空,你可以更改文本:
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if(TextString != null)
TextString.Text = textBox.Text;
}
或者,第二個選項是從 XAML 代碼的文本框中洗掉 Textchangedevent。
uj5u.com熱心網友回復:
我將補充@FrozenAssassine 的回答。您要附加 TextBox_TextChanged 兩次:
- 在 XAML 中
TextChanged="TextBox_TextChanged"; - 在后面的代碼中
textBox.TextChanged = TextBox_TextChanged;。
第一次附加(在 XAML 中),您還立即設定了 Text = "TextBox" 屬性。分配此值將立即引發 TextChanged 事件并呼叫 TextBox_TextChanged 方法。但此時 TextBlock 尚未初始化,該欄位為空。因此,您會遇到例外。
@FrozenAssassine 提出了一個解決方案。但在這種情況下,您不會初始化 TextBlock。它只會在第一次更改 TextBox 后執行。
有幾種方法可以解決它。
第一的:
<!-- Removing value assignment and event handling -->
<TextBox x:Name="textBox" HorizontalAlignment="Left" Margin="296,161,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
TextString.Text = "Hello Buddy!";
this.textBox.TextChanged = TextBox_TextChanged;
textBox.Text = "TextBox";
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextString.Text = textBox.Text;
}
}
第二:
<Grid>
<Button Content="????" HorizontalAlignment="Left" Margin="346,243,0,0" VerticalAlignment="Top" Height="35" Width="70"/>
<Border BorderThickness="1" BorderBrush="Black"/>
<!-- First the declaration of the TextBlock, and after that the TextBox. -->
<TextBlock x:Name="TextString" HorizontalAlignment="Left" Margin="459,163,0,0" Text="Dd" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Margin="296,161,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged"/>
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextString.Text = textBox.Text;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/330915.html
