msword 中的反应式扩展

发布于 2024-10-08 08:52:20 字数 1015 浏览 4 评论 0原文

我想知道是否可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给妤﹃绝世温柔 2024-10-15 08:52:20

您的问题是代表签名的问题。

定义 IObserver.OnNext作为 void(T 值)

ApplicationEvents4_DocumentBeforeSaveEventHandler 定义为 void (Document doc, ref bool SaveAsUI, ref bool Cancel)

如果您只需要发出Document(而不是其他细节,例如使其可取消),您可以执行以下操作:

public static IObservable<Word.Document> DocumentBeforeSaveAsObservable(
    this Word.Application application)
{
    return Observable.Create<Word.Document>(observer =>
    {
        Word.ApplicationEvents4_DocumentBeforeSaveEventHandler handler = 
            (doc, ref saveAsUI, ref cancel) => observer.OnNext(doc);

        application.DocumentBeforeSave += handler;

        return () => application.DocumentBeforeSave -= handler;
    });
}

如果您确实需要所有数据,则需要创建某种包装类IObservable 序列只能发出单一类型:

public class DocumentBeforeSaveEventArgs : CancelEventArgs
{
    public Document Document { get; private set; }
    public bool SaveAsUI { get; private set; }

    public DocumentBeforeSaveEventArgs(Document document, bool saveAsUI)
    {
        this.Document = document;
        this.SaveAsUI = saveAsUI;
    }
}

然后你可以像这样使用它:

public static IObservable<Word.Document> DocumentBeforeSaveAsObservable(
    this Word.Application application)
{
    return Observable.Create<Word.Document>(observer =>
    {
        Word.ApplicationEvents4_DocumentBeforeSaveEventHandler handler = 
            (doc, ref saveAsUI, ref cancel) => 
            {
                var args = new DocumentBeforeSaveEventArgs(doc, saveAsUI);

                observer.OnNext(args);

                cancel = args.Cancel;
            };

        application.DocumentBeforeSave += handler;

        return () => application.DocumentBeforeSave -= handler;
    });
}

Your problem is an issue of delegate signatures.

IObserver<T>.OnNext is defined as void (T value)

whereas ApplicationEvents4_DocumentBeforeSaveEventHandler is defined as void (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:

public static IObservable<Word.Document> DocumentBeforeSaveAsObservable(
    this Word.Application application)
{
    return Observable.Create<Word.Document>(observer =>
    {
        Word.ApplicationEvents4_DocumentBeforeSaveEventHandler handler = 
            (doc, ref saveAsUI, ref cancel) => observer.OnNext(doc);

        application.DocumentBeforeSave += handler;

        return () => application.DocumentBeforeSave -= handler;
    });
}

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:

public class DocumentBeforeSaveEventArgs : CancelEventArgs
{
    public Document Document { get; private set; }
    public bool SaveAsUI { get; private set; }

    public DocumentBeforeSaveEventArgs(Document document, bool saveAsUI)
    {
        this.Document = document;
        this.SaveAsUI = saveAsUI;
    }
}

And then you can use it like so:

public static IObservable<Word.Document> DocumentBeforeSaveAsObservable(
    this Word.Application application)
{
    return Observable.Create<Word.Document>(observer =>
    {
        Word.ApplicationEvents4_DocumentBeforeSaveEventHandler handler = 
            (doc, ref saveAsUI, ref cancel) => 
            {
                var args = new DocumentBeforeSaveEventArgs(doc, saveAsUI);

                observer.OnNext(args);

                cancel = args.Cancel;
            };

        application.DocumentBeforeSave += handler;

        return () => application.DocumentBeforeSave -= handler;
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文