{"id":929,"date":"2009-11-24T12:39:29","date_gmt":"2009-11-24T11:39:29","guid":{"rendered":"http:\/\/www.navision-blog.de\/2009\/11\/24\/generating-an-iobservablet-from-an-ievent-in-f\/"},"modified":"2009-12-21T10:34:07","modified_gmt":"2009-12-21T09:34:07","slug":"generating-an-iobservablet-from-an-ievent-in-f","status":"publish","type":"post","link":"http:\/\/www.navision-blog.de\/blog\/2009\/11\/24\/generating-an-iobservablet-from-an-ievent-in-f\/","title":{"rendered":"Generating an IObservable&lt;T&gt; from an IEvent in F#"},"content":{"rendered":"<p>Yesterday I showed how we can <a href=\"http:\/\/www.navision-blog.de\/2009\/11\/23\/mapping-the-reactive-framework-rx-operators-for-f\/\">map some of the Rx operators to an API which looks more like the F# base classes<\/a>. Today I wanted to use these mapped operators in a WPF-application written in F#. <\/p>\n<p>F# gives us a nice way to use events as first class citizen (via IEvent) but these events implement their own version of IObservable&lt;T&gt; (in FSharp.Core.dll), which is unfortunately incompatible with the Rx version and therefore with the mapped API.<\/p>\n<p>The solution I found is to wrap the F# IEvent with a Rx IObservable&lt;T&gt;:<\/p>\n<div style=\"font-family: courier new; background: white; color: black; font-size: 10pt\">\n<p style=\"margin: 0px\"><span style=\"color: green\">\/\/\/ Generates an observable from an IEvent<\/span><\/p>\n<p style=\"margin: 0px\"><span style=\"color: blue\">let<\/span> fromEvent (event:IEvent&lt;_,_&gt;) =&#160;&#160;&#160;&#160;&#160;&#160; <\/p>\n<p style=\"margin: 0px\">&#160; Observable.Create&lt;_&gt;(<span style=\"color: blue\">fun<\/span> x <span style=\"color: blue\">-&gt;<\/span><\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; event.Subscribe x.OnNext |&gt; ignore<\/p>\n<p style=\"margin: 0px\">&#160;&#160;&#160; <span style=\"color: blue\">new<\/span> System.Action(<span style=\"color: blue\">fun<\/span> () <span style=\"color: blue\">-&gt;<\/span> ()))&#160;&#160;&#160;&#160;&#160; <\/p>\n<\/p><\/div>\n<p>Now we are able to use the WPF events as observables:<\/p>\n<div style=\"font-family: courier new; background: white; color: black; font-size: 10pt\">\n<p style=\"margin: 0px\"><span style=\"color: green\">\/\/ Register ListBox Commands<\/span><\/p>\n<p style=\"margin: 0px\">listBox1.KeyDown<\/p>\n<p style=\"margin: 0px\">&#160; |&gt; Observable.fromEvent<\/p>\n<p style=\"margin: 0px\">&#160; |&gt; Observable.filter (<span style=\"color: blue\">fun<\/span> args <span style=\"color: blue\">-&gt;<\/span> args.Key = Key.Delete)<\/p>\n<p style=\"margin: 0px\">&#160; |&gt; Observable.subscribe deleteElement<\/p>\n<\/p><\/div>\n<p>I am interested if someone has a different and maybe better solution to this problem.<\/p>\n<p>Updated: 21.12.2009 \u2013 Observable.Context is no longer supported by Rx ==&gt; Removed<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday I showed how we can map some of the Rx operators to an API which looks more like the F# base classes. Today I wanted to use these mapped operators in a WPF-application written in F#. F# gives us a nice way to use events as first class citizen (via IEvent) but these events [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[448],"tags":[534,579],"_links":{"self":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/929"}],"collection":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/comments?post=929"}],"version-history":[{"count":1,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/929\/revisions"}],"predecessor-version":[{"id":938,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/929\/revisions\/938"}],"wp:attachment":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/media?parent=929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/categories?post=929"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/tags?post=929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}