Trouble with custom MarkupExtension [modified]
-
I am annoyed at having to declare icons for a menu item as static resources and then reference those resources in my menu definition, so I came up with the idea of creating a MarkupExtension called ImageLoader that inputs a relative Source (and height and width properties) and returns a fully-instantiated Image object so I can reference these inline in my menu definition:
<MenuItem
Header="Options..."
Icon="{util:ImageLoader /Resources/Icons/pi_gear_16.png, Height=16, Width=16}" />(Before I go any further, if you know of a better/easier way to do this, please tell me!) However, I'm running into an odd problem inside my MarkupExtension definition where when I pass the path into ImageSourceConverter.ConvertFrom(), it interprets it as a file path (of the default drive) rather than a resource path. I say it's odd because when I create an Image directly in XAML, I am passing the exact same string into its ImageSource attribute, and it works fine! So why is it interpreting it as a file when I do it manually?
[MarkupExtensionReturnType( typeof( Image ) )]
public class ImageLoaderExtension: MarkupExtension {public Double Width { get; set; } public Double Height { get; set; } string imagePath; public ImageLoaderExtension( string imagePath ) { this.imagePath = imagePath; } public override object ProvideValue( IServiceProvider serviceProvider ) { Image image = new Image(); image.Source = (ImageSource)( new ImageSourceConverter().ConvertFrom( imagePath ) ); image.Height = this.Height; image.Width = this.Width; return image; }
}
Thanks for any input. -Logan
modified on Thursday, September 4, 2008 7:39 PM
-
I am annoyed at having to declare icons for a menu item as static resources and then reference those resources in my menu definition, so I came up with the idea of creating a MarkupExtension called ImageLoader that inputs a relative Source (and height and width properties) and returns a fully-instantiated Image object so I can reference these inline in my menu definition:
<MenuItem
Header="Options..."
Icon="{util:ImageLoader /Resources/Icons/pi_gear_16.png, Height=16, Width=16}" />(Before I go any further, if you know of a better/easier way to do this, please tell me!) However, I'm running into an odd problem inside my MarkupExtension definition where when I pass the path into ImageSourceConverter.ConvertFrom(), it interprets it as a file path (of the default drive) rather than a resource path. I say it's odd because when I create an Image directly in XAML, I am passing the exact same string into its ImageSource attribute, and it works fine! So why is it interpreting it as a file when I do it manually?
[MarkupExtensionReturnType( typeof( Image ) )]
public class ImageLoaderExtension: MarkupExtension {public Double Width { get; set; } public Double Height { get; set; } string imagePath; public ImageLoaderExtension( string imagePath ) { this.imagePath = imagePath; } public override object ProvideValue( IServiceProvider serviceProvider ) { Image image = new Image(); image.Source = (ImageSource)( new ImageSourceConverter().ConvertFrom( imagePath ) ); image.Height = this.Height; image.Width = this.Width; return image; }
}
Thanks for any input. -Logan
modified on Thursday, September 4, 2008 7:39 PM