gpt4 book ai didi

c# - 缓冲 PropertyChanged 事件

转载 作者:行者123 更新时间:2023-11-30 17:45:38 31 4
gpt4 key购买 nike

我有一个 wpf 应用程序,其中所有 viewModel 都继承自实现 INotifyPropertyChanged 的​​类 NotifyPropertyChangeClass(见下文)。

我想限制发送到 View 的通知,以避免由于发送太多通知而导致延迟。到目前为止,我实现的是使用响应式扩展示例方法延迟每个属性。

问题是 Gui 在 throttlingPeriod 之后刷新。像这样在周期开始时引发第一个事件会感觉更灵敏:

More graphic explanation

NotifyPropertyChangeClass 代码:

using System;
using System.ComponentModel;
using System.Reactive.Linq;

namespace MyNameSpace
{
public class NotifyPropertyChangeClass : INotifyPropertyChanged
{
public NotifyPropertyChangeClass(int throttlingPeriod)
{
var obs = Observable.FromEventPattern<PropertyChangedEventHandler, PropertyChangedEventArgs>(
h => this.privatePropertyChanged += h, h => this.privatePropertyChanged -= h);
var groupedByName = obs.Select(o => o.EventArgs.PropertyName).GroupBy(x => x).SelectMany(o => o.Sample(TimeSpan.FromMilliseconds(throttlingPeriod)));
groupedByName.Subscribe(o =>
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(o));
});
}
public event PropertyChangedEventHandler PropertyChanged;
private event PropertyChangedEventHandler privatePropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = privatePropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}

我怎样才能实现我想要的?

最佳答案

我没试过,但你可以试试这个:

using System;
using System.ComponentModel;
using System.Reactive.Linq;

namespace MyNameSpace
{
public class NotifyPropertyChangeClass : INotifyPropertyChanged
{
private bool _isThrottling = false;

public NotifyPropertyChangeClass(int throttlingPeriod)
{
var obs = Observable.FromEventPattern<PropertyChangedEventHandler, PropertyChangedEventArgs>(
h => this.privatePropertyChanged += h, h => this.privatePropertyChanged -= h);
var groupedByName = obs.Select(o => o.EventArgs.PropertyName).GroupBy(x => x).SelectMany(o => o.Sample(TimeSpan.FromMilliseconds(throttlingPeriod)));
groupedByName.Subscribe(o =>
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(o));
_isThrottling = false;
});
}
public event PropertyChangedEventHandler PropertyChanged;
private event PropertyChangedEventHandler privatePropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
{
// Will fire the first time, the event is raised
if (!_isThrottling)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(o));
}

// Setting to true here will suppress raising the public event
// for every subsequent call, until the event is raised
// by the observable pattern and the flag is set to false again.
_isThrottling = true;

// Will always be raised
PropertyChangedEventHandler handler = privatePropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}

关于c# - 缓冲 PropertyChanged 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27271089/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com