Skip to content

Instantly share code, notes, and snippets.

@nigel-sampson
nigel-sampson / gist:4027391
Created November 6, 2012 20:43
TaskScheduler
var scheduler = TaskScheduler.FromCurrentSynchronizationContext();
SetUIThreadMarshaller(action =>
{
if (Window.Current != null)
action();
else
Task.Factory.StartNew(action, new CancellationToken(), TaskCreationOptions.PreferFairness, scheduler).Wait();
});
@nigel-sampson
nigel-sampson / gist:4498210
Created January 10, 2013 00:09
Returning Null
public class RetailersService : AppServiceBase
{
public async Task<IList<Retailer>> GetRetailersAsync()
{
return await BlobCache.LocalMachine.GetOrFetchObject("retailers", DownloadRetailersAsync);
}
private Task<IList<Retailer>> DownloadRetailersAsync()
{
IList<Retailer> retailers = Enumerable.Range(0, 20)
@nigel-sampson
nigel-sampson / gist:4760113
Created February 12, 2013 03:49
Collecting pointer moved and only want points once the pointer has moved a certain amount.
private void OnLoaded(object sender, RoutedEventArgs args)
{
var pointerMoved = Observable.FromEventPattern<PointerEventHandler, PointerRoutedEventArgs>(h => PathRoot.PointerMoved += h, h => PathRoot.PointerMoved -= h).Select(e => e.EventArgs);
var pointerInContact = pointerMoved.Where(p => p.Pointer.IsInContact);
var points = pointerInContact.Select(p => p.GetCurrentPoint(PathRoot)).Select(p => new Vector2((float) p.Position.X, (float) p.Position.Y));
var previousPoints = points.Skip(1);
var deltas = points.Zip(previousPoints, (current, previous) => new Vector2Delta { Point = current, Delta = current - previous });
var scannedDeltas = deltas.Scan(new Vector2Delta { Point = Vector2.Zero, Delta = Vector2.Zero }, ScanSignificantDeleta);
create_task(FileIO::ReadTextAsync(file)).then([this, file](task<String^> task)
{
try
{
String^ fileContent = task.get();
OutputTextBlock->Text = "The following text was read from '" + file->Name + "':\n\n" + fileContent;
}
catch(COMException^ ex)
{
rootPage->HandleFileNotFoundException(ex);
@nigel-sampson
nigel-sampson / gist:5536332
Created May 7, 2013 21:33
WP8 Local Machine Weirdness
var projects = await GetProjectsAsync();
await BlobCache.LocalMachine.InsertObject("projects", projects);
var test = await BlobCache.LocalMachine.GetOrCreateObject("projects", () => new List<Project>());
if (projects.Count != test.Count)
throw new InvalidOperationException("Hmmm");
@nigel-sampson
nigel-sampson / gist:5536817
Created May 7, 2013 22:51
Insert IList<T>
[TestMethod]
public async Task CanInsertInterfaces()
{
BlobCache.ApplicationName = "RepoTests";
IList<string> strings = new List<string> { "foo", "bar", "baz" }; // fails
// var = new List<string> { "foo", "bar", "baz" }; // passes
await BlobCache.LocalMachine.InsertObject("strings", strings);
@nigel-sampson
nigel-sampson / gist:8566230
Created January 22, 2014 19:57
Octokit OAuth Token Ruest
var gitHubClient = new GitHubClient(new ProductHeaderValue("Test App", "0.0.0.1"))
{
Credentials = Credentials.Anonymous
};
var content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{"client_id", ClientId },
{"client_secret", ClientSecret },
{"code", responseParameters["code"] },
public class OAuthCredentialsStore : ICredentialStore
{
private const string ClientId = "<redacted>";
private const string ClientSecret = "<redacted>";
private readonly IGitHubClient _gitHubClient;
public OAuthCredentialsStore()
{
_gitHubClient = new GitHubClient(new ProductHeaderValue("HubBug", "0.0.0.1"))
{
@nigel-sampson
nigel-sampson / App.xaml
Created October 15, 2014 19:53
Spectrum Demo
<Application
x:Class="Spectrum.Demo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:convereters="using:Spectrum.Universal.Converters">
<Application.Resources>
<convereters:ComplimentConverter x:Key="Compliment" />
<convereters:ShiftLightnessConverter x:Key="ShiftLightness" />
@nigel-sampson
nigel-sampson / gist:db65e0355b428ded315b
Created November 24, 2014 02:25
Non scrolling combo box
<ComboBox
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
ScrollViewer.HorizontalScrollMode="Disabled"
ScrollViewer.VerticalScrollMode="Disabled">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>