static void DrawCallback(IntPtr voidptr, IntPtr cgcontextptr) { GCHandle gch = GCHandle.FromIntPtr(voidptr); CGPattern container = (CGPattern)gch.Target; CGContext ctx = null; if (cgcontextptr == container.last_cgcontext_ptr) { ctx = container.last_cgcontext.Target as CGContext; } if (ctx == null) { ctx = new CGContext(cgcontextptr); container.last_cgcontext = new WeakReference(ctx); container.last_cgcontext_ptr = cgcontextptr; } container.draw_pattern(ctx); }