Skip to content

Instantly share code, notes, and snippets.

View patridge's full-sized avatar

Adam Patridge patridge

View GitHub Profile
namespace YourNamespace.Controllers {
using System.Web.Mvc;
using System.Web.SessionState;
using System;
[SessionState(SessionStateBehavior.ReadOnly)]
public class AjaxParallelController : Controller {
public JsonResult GetWait(int i = -1) {
System.Threading.Thread.Sleep(5000);
return Json(new { id = i, type = "parallel", ts = DateTime.Now.Ticks }, JsonRequestBehavior.AllowGet);
}
@patridge
patridge / gist:3907891
Created October 17, 2012 20:18
Bug: Custom UITabBarController calling ViewDidLoad from base constructor.
using System;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace TabBarFailTest {
public class Application {
static void Main(string[] args) {
try {
UIApplication.Main(args, null, "AppDelegate");
}
@patridge
patridge / ScreenshotsFromPowerPointSlides
Last active August 29, 2015 13:56
PowerPoint slide screenshots via AppleScript
tell application "Microsoft PowerPoint"
activate
set slideShow to run slide show slide show settings of active presentation
set finalShotSize to 250
set oPres to active presentation
set desktopPath to (path to desktop)
set N to 1
-- Give presentation enough time to start.
delay 0.5
@patridge
patridge / gist:8984934
Created February 13, 2014 22:08
One approach for handling keyboard show/hide in Xamarin.iOS.
NSObject keyboardShowObserver;
NSObject keyboardHideObserver;
public override void ViewWillAppear(bool animated) {
base.ViewWillAppear(animated);
keyboardShowObserver = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, (notification) => {
NSValue nsKeyboardBounds = (NSValue)notification.UserInfo.ObjectForKey(UIKeyboard.BoundsUserInfoKey);
RectangleF keyboardBounds = nsKeyboardBounds.RectangleFValue;
float height = View.Bounds.Height - keyboardBounds.Height;
if (NavigationController != null && NavigationController.TabBarController != null && NavigationController.TabBarController.TabBar != null) {
@patridge
patridge / gist:10499338
Last active August 23, 2018 15:52
Xamarin: get a random color
// Xamarin.iOS (using MonoTouch.UIKit;)
static Random rand = new Random();
public static UIColor GetRandomColor() {
int hue = rand.Next(255);
UIColor color = UIColor.FromHSB(
(hue / 255.0f),
1.0f,
1.0f);
return color;
}
@patridge
patridge / MainActivity.cs
Created September 19, 2014 15:47
Core reproduction code for `NoSuchMethodError` in Google Analytics code using Google Play Services Xamarin Component (v19.0.0.1)
using System;
using Android.App;
using Android.Content;
using Android.Gms.Analytics; // via Google Play Services Xamarin Component (v19.0.0.1)
using Android.Gms.Analytics.Ecommerce;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
public static class TaskHelper {
public static Task CompletedTask = AsCompletedTask(true);
public static Task<T> AsCompletedTask<T>(T result) {
TaskCompletionSource<T> precompletedSource = new TaskCompletionSource<T>();
precompletedSource.SetResult(result);
return precompletedSource.Task;
}
public static void TrySetResultTask<TResult>(this TaskCompletionSource<TResult> tcs, Task<TResult> task) {
if (task == null) {
throw new ArgumentNullException("task");
@patridge
patridge / AppDelegate.cs
Created February 5, 2015 17:05
Sample: BTProgressHUD blocking tab bar
using System;
using Foundation;
using UIKit;
using CoreGraphics;
using BigTed;
using System.Threading.Tasks;
using System.Linq;
using ObjCRuntime;
namespace HudOverTabsSample {
@patridge
patridge / gist:2ca7e309cd53716fb384
Created February 17, 2015 15:38
Android: what ID is that thing?
// I had to use this to debug why `OnOptionsItemSelected` wasn't triggering for an ActionBar home tap.
// Turns out, `Resource.Id.home` and `Resource.Id.homeAsUp` are not related to `Android.Resource.Id.Home`.
var projectIdType = typeof(Resource.Id);
foreach (var field in projectIdType.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)) {
if (item.ItemId == (int)field.GetValue(null)) {
Console.WriteLine("{0}: {1}", field.Name, item.ItemId);
}
}
var androidIdType = typeof(Android.Resource.Id);
@patridge
patridge / UITableViewWithoutLayoutMargins.cs
Last active August 29, 2015 14:15
UITableView and UITableViewCell without the iOS 7/8 margins.
// LICENSE: MIT
public class UITableViewWithoutLayoutMargins : UITableView {
void InitializeWithoutMargins() {
// iOS 7
if (RespondsToSelector(new Selector("setSeparatorInset:"))) {
SeparatorInset = UIEdgeInsets.Zero;
}
// iOS 8[+?]
if (RespondsToSelector(new Selector("setLayoutMargins:"))) {
LayoutMargins = UIEdgeInsets.Zero;