这个问题在这里已经有了答案:
Need help preventing infinite loop. Setting of Properties
(3 个回答)
1年前关闭。
我陷入了无限循环,我认为问题是 XAML TextBox 中的 UpdateSourceTrigger=PropertyChanged。
我想要实现的是:
但我的问题是 UpdateSourceTrigger=PropertyChanged 触发了一系列导致我的应用程序无限循环或崩溃的事件。
我的 XAML 代码(查看):
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBox
Grid.Row="0"
Grid.Column="0"
Margin="5,0,5,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Bottom"
CaretBrush="#FF202020"
SelectionBrush="#FF202020"
Text="{Binding FloatInput, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" />
<TextBox
Grid.Row="0"
Grid.Column="1"
Margin="5,0,5,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Bottom"
CaretBrush="#FF202020"
SelectionBrush="#FF202020"
Text="{Binding HartInput, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" />
<TextBox
Grid.Row="0"
Grid.Column="2"
Margin="5,0,5,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Bottom"
CaretBrush="#FF202020"
SelectionBrush="#FF202020"
Text="{Binding GDCInput}" />
</Grid>
我的 VieModel 代码:public class FloatHartGdcConverterViewModel: FoundationViewModel
{
private string _floatInput;
public string FloatInput
{
get { return _floatInput; }
set
{
_floatInput = value;
//HartInput = GeneralConverters.HexaToFloat(_floatInput);
OnPropertyChanged();
}
}
private string _hartInput;
public string HartInput
{
get { return _hartInput; }
set
{
_hartInput = value;
// FloatInput = GeneralConverters.FloatToHexa(_hartInput);
OnPropertyChanged();
}
}
private string _gdcInput;
public string GDCInput
{
get { return _gdcInput; }
set { _gdcInput = value; }
}
}
最佳答案
由于您在同一个类(class),您可以直接写入“_”私有(private)值,然后抛出您的 OnPropertyChanged()。由于您没有申请 SETTER 组件,因此它不会递归调用其他组件。
关于c# - 防止由于 "PropertyChanged - events"导致的无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65457595/