c# - 防止由于 "PropertyChanged - events"导致的无限循环

标签 c# xaml mvvm

这个问题在这里已经有了答案:





Need help preventing infinite loop. Setting of Properties

(3 个回答)


1年前关闭。




我陷入了无限循环,我认为问题是 XAML TextBox 中的 UpdateSourceTrigger=PropertyChanged。
我想要实现的是:

  • 我有 3 个文本框:
  • 1 -> 输入一个浮点数,这会在文本框 2(大端)和文本框 3(小端)中触发转换为十六进制值。
  • 2 -> 在文本框 2 中输入一个十六进制值,这会触发文本框 1 中的浮点转换和文本框 3 中等效的 little endia
  • 3 ->相同的行为。

  • 但我的问题是 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/

    相关文章:

    c# - 为什么我的 XAML 交互行为在用户控件中不起作用

    c# - .Net 核心启动基类

    c# - 带有 < 和 > 的正则表达式

    c# - 从不同的线程关闭启动画面?

    c# - 在 Windows 中以编程方式确定电源使用情况?

    c# - 从 Windows 8 应用商店应用程序项目文件夹中移动 App.xaml

    XAML 按钮导致 Xamarin 应用程序在启动时崩溃

    .net - 在列表框 WPF 中设置选定项周围的边框

    WPF MVVM - 将事物移动到 View 模型时遇到问题

    xaml - CalendarDatePicker 在第二次选择相同的日期时返回 null