WindowsObservable.Events.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. #if HAS_WINRT
  5. using System.Threading;
  6. using Windows.Foundation;
  7. namespace System.Reactive.Linq
  8. {
  9. /// <summary>
  10. /// Provides a set of static methods for importing typed events from Windows Runtime APIs.
  11. /// </summary>
  12. public static partial class WindowsObservable
  13. {
  14. /// <summary>
  15. /// Converts a typed event, conforming to the standard event pattern, to an observable sequence.
  16. /// </summary>
  17. /// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
  18. /// <typeparam name="TResult">The type of the event data generated by the event.</typeparam>
  19. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  20. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  21. /// <returns>The observable sequence that contains data representations of invocations of the underlying typed event.</returns>
  22. /// <exception cref="ArgumentNullException"><paramref name="addHandler"/> or <paramref name="removeHandler"/> is null.</exception>
  23. /// <seealso cref="WindowsObservable.ToEventPattern"/>
  24. public static IObservable<EventPattern<TSender, TResult>> FromEventPattern<TSender, TResult>(Action<TypedEventHandler<TSender, TResult>> addHandler, Action<TypedEventHandler<TSender, TResult>> removeHandler)
  25. {
  26. if (addHandler == null)
  27. throw new ArgumentNullException("addHandler");
  28. if (removeHandler == null)
  29. throw new ArgumentNullException("removeHandler");
  30. return Observable.Create<EventPattern<TSender, TResult>>(observer =>
  31. {
  32. var h = new TypedEventHandler<TSender, TResult>((sender, args) =>
  33. {
  34. observer.OnNext(new EventPattern<TSender, TResult>(sender, args));
  35. });
  36. addHandler(h);
  37. return () =>
  38. {
  39. removeHandler(h);
  40. };
  41. });
  42. }
  43. /// <summary>
  44. /// Converts a typed event, conforming to the standard event pattern, to an observable sequence.
  45. /// </summary>
  46. /// <typeparam name="TDelegate">The delegate type of the event to be converted.</typeparam>
  47. /// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
  48. /// <typeparam name="TResult">The type of the event data generated by the event.</typeparam>
  49. /// <param name="conversion">A function used to convert the given event handler to a delegate compatible with the underlying typed event. The resulting delegate is used in calls to the addHandler and removeHandler action parameters.</param>
  50. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  51. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  52. /// <returns>The observable sequence that contains data representations of invocations of the underlying typed event.</returns>
  53. /// <exception cref="ArgumentNullException"><paramref name="conversion"/> or <paramref name="addHandler"/> or <paramref name="removeHandler"/> is null.</exception>
  54. /// <seealso cref="WindowsObservable.ToEventPattern"/>
  55. public static IObservable<EventPattern<TSender, TResult>> FromEventPattern<TDelegate, TSender, TResult>(Func<TypedEventHandler<TSender, TResult>, TDelegate> conversion, Action<TDelegate> addHandler, Action<TDelegate> removeHandler)
  56. {
  57. if (conversion == null)
  58. throw new ArgumentNullException("conversion");
  59. if (addHandler == null)
  60. throw new ArgumentNullException("addHandler");
  61. if (removeHandler == null)
  62. throw new ArgumentNullException("removeHandler");
  63. return Observable.Create<EventPattern<TSender, TResult>>(observer =>
  64. {
  65. var h = conversion(new TypedEventHandler<TSender, TResult>((sender, args) =>
  66. {
  67. observer.OnNext(new EventPattern<TSender, TResult>(sender, args));
  68. }));
  69. addHandler(h);
  70. return () =>
  71. {
  72. removeHandler(h);
  73. };
  74. });
  75. }
  76. /// <summary>
  77. /// Exposes an observable sequence as an object with a typed event.
  78. /// </summary>
  79. /// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
  80. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  81. /// <param name="source">Observable source sequence.</param>
  82. /// <returns>The event source object.</returns>
  83. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  84. public static IEventPatternSource<TSender, TEventArgs> ToEventPattern<TSender, TEventArgs>(this IObservable<EventPattern<TSender, TEventArgs>> source)
  85. {
  86. if (source == null)
  87. throw new ArgumentNullException("source");
  88. return new EventPatternSource<TSender, TEventArgs>(source, (h, evt) => h(evt.Sender, evt.EventArgs));
  89. }
  90. }
  91. }
  92. #endif