Skip to content

Instantly share code, notes, and snippets.

@luojunyuan
Created November 26, 2025 07:12
Show Gist options
  • Select an option

  • Save luojunyuan/1ad42294305850e42ccac63034adc69b to your computer and use it in GitHub Desktop.

Select an option

Save luojunyuan/1ad42294305850e42ccac63034adc69b to your computer and use it in GitHub Desktop.
Avalonia GetObservable for WPF
extension(DependencyObject obj)
{
public Observable<T> GetObservable<T>(DependencyProperty property)
{
var descriptor = DependencyPropertyDescriptor.FromProperty(property, obj.GetType());
return Observable
.FromEventHandler(
h => descriptor.AddValueChanged(obj, h),
h => descriptor.RemoveValueChanged(obj, h))
.Select(_ => (T)obj.GetValue(property))
.Prepend((T)obj.GetValue(property));
}
}
@luojunyuan
Copy link
Author

注意 DependencyProperty 是个静态的东西,一定需要释放。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment