c# – System.StackOverflowException这没有意义……如何调试?

前端之家收集整理的这篇文章主要介绍了c# – System.StackOverflowException这没有意义……如何调试?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在构建一个使用简单MVVM架构和EF的 WPF应用程序.

我看到一个奇怪的问题,如果我尝试设置datetime属性,我得到一个System.StackOverflowException.如果我没有设置datetime属性,我不会得到异常.

捆绑:

<DatePicker Style="{StaticResource Dp}" 
               Grid.Column="1" 
               SelectedDate="{Binding Date,UpdateSourceTrigger=PropertyChanged}" />

公共财产:

public DateTime Date
{
    get
    {
        return _criticalDate.Date;
    }
    set
    {
        if (_criticalDate != null && value != null && _criticalDate.Date == value)
            return;
        _criticalDate.Date = value;
        OnPropertyChanged("Date");
    }
}

尝试使用调试器逐步执行它似乎不起作用.我已经看过所有试图看到发生了什么的事情……有什么迹象表明可能导致这种情况发生了什么?

这是CriticalDate类的定义,

public partial class CriticalDate
{
    public int ID { get; set; }
    public System.DateTime Date { get; set; }
    public string CriticalReason { get; set; }
    public int FileID { get; set; }
}

_criticalDate字段是CriticalDate类的私有实例. CriticalDate是EF从我的数据库模式创建的类.它本身不是DateTime.

最终更新

我仍然不知道出了什么问题……我撕掉了有问题的部分(包括绑定)并从头开始重写.不知道我做了什么不同,但它现在有效.我认为这与物品控制的设置方式有关…如果我有更多的时间(愚蠢的截止日期),我会回去看看它是什么,但现在这是一个谜.

感谢您分享我的困惑,如果只是如此简短.

解决方法

尝试删除OnPropertyChanged(“Date”);或将绑定模式更改为OneWayToSource.

documentation of UpdateSourceTrigger.

Bindings that are TwoWay or OneWayToSource listen for changes in the target property and propagate them back to the source. This is known as updating the source. Usually,these updates happen whenever the target property changes. This is fine for check Boxes and other simple controls,but it is usually not appropriate for text fields. Updating after every keystroke can diminish performance and it denies the user the usual opportunity to backspace and fix typing errors before committing to the new value. Therefore,the default UpdateSourceTrigger value of the Text property is LostFocus and not PropertyChanged.

我相信OnPropertyChanged(“日期”);在Date_set方法上更新UI,然后再次调用Date_set方法,完成循环并导致递归.

原文链接:https://www.f2er.com/csharp/99725.html

猜你在找的C#相关文章