微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/csharp/99725.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐