msword 中的反应式扩展
我想知道是否可以在 Word 中使用反应式扩展。我已经看到 Jeff 展示了如何在 Excel 中连接工作簿打开事件 http://social.msdn.microsoft.com/Forums/en/rx/thread/5ace45b1-778b-4ddd-b2ab-d5c8a1659f5f。
我想知道我是否可以用文字做同样的事情。
我已经走到这一步了....
public static class ApplicationExtensions
{
public static IObservable<Word.Document> DocumentBeforeSaveAsObservable(this Word.Application application)
{
return Observable.Create<Word.Document>(observer =>
{
Word.ApplicationEvents4_DocumentBeforeSaveEventHandler handler = observer.OnNext;
application.DocumentBeforeSave += handler;
return () => application.DocumentBeforeSave -= handler;
});
}
}
但我收到错误“No override for 'OnNext' matches delegate 'Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeSaveEventHandler”
任何人都可以指出我正确的方向。
问候
迈克
I am wondering if it is possible to use Reactive Extensions in Word. I have seen this where Jeff shows how to wire up a workbook open event in excel http://social.msdn.microsoft.com/Forums/en/rx/thread/5ace45b1-778b-4ddd-b2ab-d5c8a1659f5f.
I wondering if I could do the same sort of thing in word.
I have got this far....
public static class ApplicationExtensions
{
public static IObservable<Word.Document> DocumentBeforeSaveAsObservable(this Word.Application application)
{
return Observable.Create<Word.Document>(observer =>
{
Word.ApplicationEvents4_DocumentBeforeSaveEventHandler handler = observer.OnNext;
application.DocumentBeforeSave += handler;
return () => application.DocumentBeforeSave -= handler;
});
}
}
but I receive the error No overload for 'OnNext' matches delegate 'Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeSaveEventHandler
Can anyone point me in the right direction.
Regards
Mike
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是代表签名的问题。
定义
IObserver.OnNext
作为void(T 值)
而
ApplicationEvents4_DocumentBeforeSaveEventHandler
定义为void (Document doc, ref bool SaveAsUI, ref bool Cancel)
如果您只需要发出
Document
(而不是其他细节,例如使其可取消),您可以执行以下操作:如果您确实需要所有数据,则需要创建某种包装类
IObservable
序列只能发出单一类型:然后你可以像这样使用它:
Your problem is an issue of delegate signatures.
IObserver<T>.OnNext
is defined asvoid (T value)
whereas
ApplicationEvents4_DocumentBeforeSaveEventHandler
is defined asvoid (Document doc, ref bool SaveAsUI, ref bool Cancel)
If you only need to emit the
Document
(and not the other details, like making it cancelable), you can do something like this:If you do require all the data, you'll need to create a wrapper class of some kind an
IObservable
sequence can only emit a single type:And then you can use it like so: