gpt4 book ai didi

c# - 无法计算表达式,因为当前线程处于堆栈溢出状态

转载 作者:行者123 更新时间:2023-12-02 22:34:16 25 4
gpt4 key购买 nike

在我学习的过程中......我创建了一个简单的数据绑定(bind)项目,它可以很好地处理一段数据,例如名。但是,当我尝试使用 lastName 时,编译器会抛出运行时错误

** 无法计算表达式,因为当前线程处于堆栈溢出状态。**

这是代码。如您所见,第二个字段(姓氏)被注释掉,因为它会导致堆栈溢出。任何意见表示赞赏。

public partial class MainWindow : Window
{
Person p;

public MainWindow()
{
InitializeComponent();

p = new Person();
p.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(p_PropertyChanged);

this.DataContext = p;

p.FirstName = p.OriginalFirstName;
p.LastName = p.OriginalLastName;
}

void p_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
stat1.Text = (p.OriginalFirstName == p.FirstName) ? "Original" : "Modified";
//stat2.Text = (p.OriginalLastName == p.LastName) ? "Original" : "Modifined";
}

}

编辑:

 class Person : INotifyPropertyChanged 
{

public string OriginalFirstName = "Jim";
public string OriginalLastName = "Smith";

private string _firstName;

#region FirstName
public string FirstName
{
get { return _firstName; }
set
{
if (value != null)
{
_firstName = value;
NotifyTheOtherGuy(FirstName);
}
}
}
#endregion FirstName

private string _lastName;

#region LastName
public string LastName
{
get { return _lastName; }
set
{
if (value != null)
{
_lastName = value;
NotifyTheOtherGuy(LastName);
}
}
}
#endregion LastName

public Person()
{

}

public event PropertyChangedEventHandler PropertyChanged;
void NotifyTheOtherGuy(string msg)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(msg));
}

}

}

XAML:

<Window x:Class="FullNameDataBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>

<Label Grid.Column="0" Grid.Row="0" Content="First Name:"/>
<Label Grid.Row="1" Content="Last Name:"/>
<TextBox Grid.Column="1" Grid.Row="0" Background="Yellow" Margin="5" FontWeight="Bold" Text="{Binding Path=FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock x:Name="stat1" Grid.Column="2" />
<TextBox x:Name="stat2" Grid.Column="1" Grid.Row="1" Background="Yellow" Margin="5" FontWeight="Bold" Text="{Binding Path=LastName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Grid.Column="2" Grid.Row="1" />
</Grid>
</Window>

最佳答案

我认为您的 XAML 的这 block 错误:

    <TextBox  Grid.Column="1" Grid.Row="0" Background="Yellow" Margin="5" FontWeight="Bold" Text="{Binding Path=FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock x:Name="stat1" Grid.Column="2" />
<TextBox x:Name="stat2" Grid.Column="1" Grid.Row="1" Background="Yellow" Margin="5" FontWeight="Bold" Text="{Binding Path=LastName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Grid.Column="2" Grid.Row="1" />

我认为您希望最后一个 TextBlock 具有 x:Name="stat2",而不是它之前的 TextBox

当您更改 LastName 时,您的 PropertyChanged 事件处理程序将被调用,它会更改 stat2 的文本值。因为 stat2TextBox,其值使用 TwoWay 绑定(bind)绑定(bind)到 LastName,这导致绑定(bind)机制将您设置的值发送回 View 模型。这会导致另一个 PropertyChanged 事件触发,这会更改 stat2 的值,这会导致另一个 PropertyChanged 事件触发......这个无限循环不会停止,这就是你得到堆栈溢出的原因错误。

FirstName 不会导致任何此类堆栈溢出,因为 stat1 是一个 TextBlock,其 Text< 没有绑定(bind) 属性。

关于c# - 无法计算表达式,因为当前线程处于堆栈溢出状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11694309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com