From 390871cf36d8df4dd801c103d25af77357226d7d Mon Sep 17 00:00:00 2001 From: Vincent Dondain Date: Wed, 27 Apr 2016 00:45:03 -0400 Subject: [PATCH] [appkit] Add NSImage lazy initialization We were missing initByReferencingFile: which initializes the image object lazily. The non working use case was: var iconFile = NSBundle.MainBundle.PathForResource ("AppIcons", "icns"); NSApplication.SharedApplication.ApplicationIconImage = new NSImage (iconFile); In this example, the constructor is calling initWithContentsOfFile: which will not load the right icon from the .icns file. Fixes https://bugzilla.xamarin.com/show_bug.cgi?id=40349. --- src/AppKit/NSImage.cs | 8 ++++++++ src/appkit.cs | 5 +++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/AppKit/NSImage.cs b/src/AppKit/NSImage.cs index 7bb76bb803e5..b514b37b1a81 100644 --- a/src/AppKit/NSImage.cs +++ b/src/AppKit/NSImage.cs @@ -88,6 +88,14 @@ public static NSImage FromStream (System.IO.Stream stream) } } + public NSImage (string fileName, bool lazy) + { + if (lazy) + Handle = InitByReferencingFile (fileName); + else + Handle = new NSImage (fileName).Handle; + } + public NSImage (NSData data, bool ignoresOrientation) { if (ignoresOrientation) { diff --git a/src/appkit.cs b/src/appkit.cs index 3b9e5e0cd558..28255d5e0ce9 100644 --- a/src/appkit.cs +++ b/src/appkit.cs @@ -8183,8 +8183,6 @@ public partial interface NSImage : NSCoding, NSCopying, NSSecureCoding, NSPasteb [Export ("initWithContentsOfURL:")] IntPtr Constructor (NSUrl url); - //[Export ("initByReferencingFile:")] - //IntPtr Constructor (string fileName); //[Export ("initByReferencingURL:")] //IntPtr Constructor (NSUrl url); @@ -8192,6 +8190,9 @@ public partial interface NSImage : NSCoding, NSCopying, NSSecureCoding, NSPasteb //[Export ("initWithIconRef:")] //IntPtr Constructor (IconRef iconRef); + [Export ("initByReferencingFile:"), Internal] + IntPtr InitByReferencingFile (string name); + [Export ("initWithPasteboard:")] IntPtr Constructor (NSPasteboard pasteboard);