あなたのプロパティは、バッキングフィールドに値を設定し、変更通知を上げる以外にないSHOLD。Observableを通じてこれらの変更通知を聞いて、他のロジックはすべての外に置く必要があります。
特定の時間が経過した後にのみ通知を聞きたいので、IObservable.Throttleオペレータを使用して、通知を転送する前に特定の時間間隔を待つことができます。
// Just check if value is different from the actual backing field.
// If and only if it is, set the backing field and raise the PropertyChanged event.
public string SearchText
{
get { return _SearchText; }
set
{
if (!EqualityComparer<string>.Default.Equals(_SearchText, value))
{
_SearchText = value;
NotifyPropertyChanged();
}
}
}
private string _SearchText;
最初のソリューション::ViewModelにスロットル:
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
これは、あなたがすべてのプロパティを記述する必要がある方法です。
だから、これはINotifyPropertyChangedの単純な形で実装することができ
// Transform the PropertyChanged event in an Observable
var changed = Observable.FromEventPattern<PropertyChangedEventHandler, PropertyChangedEventArgs>(
h => PropertyChanged += h,
h => PropertyChanged -= h);
// Transform the Observable to your need,
// filtering only when SearchText changes
changed.Where(x => x.EventArgs.PropertyName == nameof(SearchText))
// Wait 500 ms before forwarding the notification;
// if another char in inserted from the user beside this interval,
// the previous value is ignored
.Throttle(TimeSpan.FromMilliseconds(500))
// Necessary to avoid cross-thread Exceptions
.ObserveOn(Scheduler.CurrentThread)
// finally, subscribe your method:
.Subscribe(_ => MyMethod());
コード上記eはどこ時宜です:ViewModelにのコンストラクタで、Init()
方法で...
SECOND SOLUTION:それは同じ結果を持っているバインディング
Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged, Delay=500}"
の遅延プロパティを使用します。