Skip to content

Instantly share code, notes, and snippets.

@ChrisBriggsy
Last active September 8, 2015 10:22
Show Gist options
  • Save ChrisBriggsy/2300f56973636dd774a1 to your computer and use it in GitHub Desktop.
Save ChrisBriggsy/2300f56973636dd774a1 to your computer and use it in GitHub Desktop.
Setting up Dependency Injection for Windows Universal Platform using Autofac - Step 6 - Wiring it all up
using System;
using System.Collections.Generic;
using System.Net.Http;
using Windows.UI.Xaml.Controls;
using Autofac;
using SparkFun;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace IoTBackUp
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
var builder = new ContainerBuilder();
#if (PI)
builder.RegisterType<SparkFunWeatherSheild>().As<ISparkFunWeatherSheild>();
#else
builder.RegisterType<DesktopWeather>().As<ISparkFunWeatherSheild>();
#endif
Container = builder.Build();
InitializeComponent();
Display();
}
private static IContainer Container { get; set; }
private async void Display()
{
using (var scope = Container.BeginLifetimeScope())
{
var sheild = scope.Resolve<ISparkFunWeatherSheild>();
if (await sheild.Setup())
{
Temperature.Text = sheild.Temperature.ToString();
Humdity.Text = sheild.Humidity.ToString();
PostToAzure(sheild);
}
}
}
private async void PostToAzure(ISparkFunWeatherSheild sheild)
{
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("DeviceID", Guid.NewGuid().ToString()),
new KeyValuePair<string, string>("Humidity", sheild.Humidity.ToString()),
new KeyValuePair<string, string>("Temperature", sheild.Temperature.ToString())
});
var client = new HttpClient();
var response = await client.PostAsync("http://yourWeatherAPI.azurewebsites.net/api/weather", formContent);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment