Skip to content

Instantly share code, notes, and snippets.

Weekend project: A little Nancy (http://nancyfx.org) extension which exposes your methods in C# as async calls in Javascript. I really like what Now.js is doing for RPC on Node and want to clone it, so this weekend's task was a fun proof of concept.
All you need to do is subclass my thing and write some public methods:<br>
http://i.imgur.com/NnqQo.png
Then in your page, include 'nanio.js' which is emitted by my lib (and JQuery, currently the sole external dependency since I'm not currently interested in rewriting ajax handling
http://i.imgur.com/TNlhX.png
And then your calls work! Yay!
http://i.imgur.com/1TuZa.png
<!DOCTYPE html>
<html>
<body>
<pre><script type="text/javascript">
try {
document.createElement("div").style.setProperty("width", 216, "");
document.write("CSSStyleDeclaration's setProperty coerces strings. Hooray!\n");
} catch (error) {
document.write("CSSStyleDeclaration's setProperty requires strings. Patching…\n");
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Default
{
public static void Main(string[] args)
{
Console.WriteLine("how you like these nuts");
Observable.FromEvent((EventHandler<SensorReadingEventArgs<CompassReading>> e) => new EventHandler<SensorReadingEventArgs<CompassReading>>(e),
e => compass.CurrentValueChanged += e,
e => compass.CurrentValueChanged -= e)
using System.Diagnostics;
using Microsoft.Devices.Sensors;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Reactive;
namespace Rx_Compass_Smoothing
{
public partial class MainPage : PhoneApplicationPage
{
Compass compass = new Compass();
using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.Devices.Sensors;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Reactive;
namespace Rx_Compass_Smoothing
{
public partial class MainPage : PhoneApplicationPage
Observable.FromEvent<SensorReadingEventArgs<CompassReading>>(compass, "CurrentValueChanged")
.BufferWithTime(TimeSpan.FromSeconds(2))
.SelectMany(events => events.Select(e => e.EventArgs.SensorReading.TrueHeading))
Observable.FromEvent<SensorReadingEventArgs<CompassReading>>(compass, "CurrentValueChanged")
.BufferWithTime(TimeSpan.FromSeconds(2))
.Select(headings => headings.Select(e => e.EventArgs.SensorReading.TrueHeading).Sum() / headings.Count())
.Subscribe(heading => Debug.WriteLine(string.Format("Averaged: {0}", heading)));
using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.Devices.Sensors;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Reactive;
namespace Rx_Compass_Smoothing
{
public partial class MainPage : PhoneApplicationPage
<Image Stretch="None" Source="compass.arrow.dark.png" RenderTransformOrigin="0.5,0.5">
<Image.RenderTransform>
<RotateTransform x:Name="ArrowRotation" CenterX="0.5" CenterY="0.5" />
</Image.RenderTransform>
</Image>