Skip to content

Instantly share code, notes, and snippets.

View programmation's full-sized avatar

David Dancy programmation

View GitHub Profile
public static class EnumExtensions
{
public static string Description(this Enum e)
{
var type = e.GetType();
var name = Enum.GetName(type, e);
var memberInfo = type.GetTypeInfo()
.DeclaredMembers
.Where(p => p.Name == name)
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
namespace MyNamespace
{
public class LoggingHttpClientHandler : HttpClientHandler
{
var response = await GetAsync (requestUri, token);
var responseType = response.Content?.Headers?.ContentType?.MediaType;
this.Debug (String.Format ("Response {0} ({1}) -> {2}", (int)response.StatusCode, response.StatusCode.ToString (), responseType));
var responseText = await response.Content.ReadAsStringAsync ();
private int GetNavBarHeight()
{
int resourceId = Resources.GetIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0)
{
return Resources.GetDimensionPixelSize(resourceId);
}
return 0;
}
@programmation
programmation / ExemptEncryptionKey.xml
Created March 7, 2016 04:38
Apple iOS exempt encryption key for use in info.plist
<key>ITSAppUsesNonExemptEncryption</key><false/>
@programmation
programmation / GetPropertyInPCL.cs
Created February 11, 2016 21:50
GetProperty equivalent code in PCL
// https://forums.xamarin.com/discussion/comment/180081/#Comment_180081
// Platform project
{
var type = item.GetType();
var prop = type.GetProperty(picker.DisplayMember);
picker.Items.Add(prop.GetValue(item).ToString());
}
// PCL
{
@programmation
programmation / AsyncMixin
Last active October 20, 2015 04:45
Async mixin utilities
using System;
using System.Threading.Tasks;
namespace MyApp
{
// Borrowed from @rid00z (http://www.michaelridland.com)
public static class AsyncMixin
{
public static void RunForget(this Task t)
{
@programmation
programmation / BeginInvokeOnMainThreadAsync.cs
Created September 23, 2015 23:01
Async BeginInvokeOnMainThread returning a result
// from https://forums.xamarin.com/discussion/comment/154636/#Comment_154636
public static Task<T> BeginInvokeOnMainThreadAsync<T>(Func<T> a)
{
var tcs = new TaskCompletionSource<T>();
Device.BeginInvokeOnMainThread(() =>
{
try
{
var result = a();
tcs.SetResult(result);
@programmation
programmation / shake.cs
Created September 18, 2015 07:24
Xamarin Forms View extension (shake)
public static void Shake (this View view, int shakes = 4, Action completion = null)
{
Task.Run (() => {
Device.BeginInvokeOnMainThread (async () => {
uint duration = 50;
await view.TranslateTo (5.0, 0, duration, Easing.Linear);
for (var shake = 1; shake < shakes - 1; shake++) {
await view.TranslateTo (-10.0, 0, duration, Easing.Linear);
await view.TranslateTo (10.0, 0, duration, Easing.Linear);
}
@programmation
programmation / wobble.cs
Created September 18, 2015 07:04
Xamarin Forms View extension (wobble)
public static class ViewExtensions
{
public static void Wobble (this View view, int wobbles = 4, Action completion = null)
{
Task.Run (() => {
Device.BeginInvokeOnMainThread (async () => {
uint duration = 50;
var count = 4;
await view.RelRotateTo (5.0, duration, Easing.Linear);
for (var shake = 1; shake < count - 1; shake++) {