gpt4 book ai didi

wpf - 错误无法在订阅 IObservable 时转换 lambda 表达式

转载 作者:行者123 更新时间:2023-12-03 10:34:57 24 4
gpt4 key购买 nike

我正在尝试使用 Rx 在 wpf 中实现标准的拖放图像。

var mouseDown = from evt in Observable.FromEventPattern<MouseButtonEventArgs>(image, "MouseLeftButtonDown")
select evt.EventArgs.GetPosition(image);

var mouseUp = Observable.FromEventPattern<MouseButtonEventArgs>(this, "MouseLeftButtonUp");

var mouseMove = from evt in Observable.FromEventPattern<MouseEventArgs>(this, "MouseMove")
select evt.EventArgs.GetPosition(this);

var q = from startLocation in mouseDown
from endLocation in mouseMove.TakeUntil(mouseUp)
select new Point
{
X = endLocation.X - startLocation.X,
Y = endLocation.Y - startLocation.Y
};

q.ObserveOn(SynchronizationContext.Current).Subscribe(point =>
{
Canvas.SetLeft(image, point.X);
Canvas.SetTop(image, point.Y);
});

我收到错误错误 Cannot convert lambda expression to type 'System.IObserver<System.Windows.Point>' because it is not a delegate type
我错过了什么?

最佳答案

命名空间 System.Reactive.Linq包含静态类 Observable它定义了常见响应式(Reactive)组合器的所有扩展方法。它位于 System.Reactive.dllIObservable<T>.Subscribe的扩展方法如 Subscribe(onNext) , Subscribe(onNext, onError)然而在 mscorlib 中定义在静态类中 System.ObservableExtensions .

tl;博士:

  • 对于 Rx/Observable 扩展方法,您需要导入 System.Reactive.Linq = using System.Reactive.Linq;
  • 对于订阅重载,您需要导入 System = using System;
  • 关于wpf - 错误无法在订阅 IObservable<Point> 时转换 lambda 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9454030/

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