加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 07:51:44 所属栏目:百科 来源:网络整理
导读:我正在构建一个使用简单MVVM架构和EF的 WPF应用程序. 我看到一个奇怪的问题,如果我尝试设置datetime属性,我得到一个System.StackOverflowException.如果我没有设置datetime属性,我不会得到异常. 捆绑: DatePicker Style="{StaticResource Dp}" Grid.Column=
我正在构建一个使用简单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方法,完成循环并导致递归.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读