如何附加属性(通过.NET infrastracture或其他方式)
我有一个WPF应用程序,我在项目资源中添加了许多图标和位图 现在我可以这样访问它们:如何附加属性(通过.NET infrastracture或其他方式),.net,properties,infrastructure,.net,Properties,Infrastructure,我有一个WPF应用程序,我在项目资源中添加了许多图标和位图 现在我可以这样访问它们: Dim ico As System.Drawing.Icon = My.Resources.Icon 'Icon.ico Dim img As System.Drawing.Bitmap = My.Resources.Image 'Image.png 为了在wpf中使用它,我创建了太简单的扩展方法,将它们转换为ImageSource类型: '...Imports System.Drawing '...Impo
Dim ico As System.Drawing.Icon = My.Resources.Icon 'Icon.ico
Dim img As System.Drawing.Bitmap = My.Resources.Image 'Image.png
为了在wpf中使用它,我创建了太简单的扩展方法,将它们转换为ImageSource类型:
'...Imports System.Drawing
'...Imports System.Windows.Interop.Imaging
<Extension()> _
Public Function ToImageSource(ByVal icon As Icon) As BitmapSource
Return CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty, _
BitmapSizeOptions.FromEmptyOptions)
End Function
<Extension()> _
Public Function ToImageSource(ByVal image As Bitmap) As BitmapSource
Return CreateBitmapSourceFromHBitmap(image.GetHbitmap(), IntPtr.Zero, _
Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions)
End Function
通过查看MyWpfExtensions.vb,我发现很少有Microsoft基础架构允许非官方编码,下面向各位专家提出我的问题
对于System.Drawing.Bitmap/Icon类型的每个资源,我希望有一个附加的或重写的属性,该属性通过ex.方法返回图像,因此我不必在Xaml中使用转换器,而是直接使用它
我实际上在寻找类似Microsoft.VisualBasic.MyGroupCollectionAttribute的东西
有什么想法吗?…我想除了一个转换器之外没有其他方法,所以让我们发布它:
Imports System.Drawing
Namespace Converters
<ValueConversion(GetType(MarshalByRefObject), GetType(BitmapSource))> _
Public Class ImageSourceConverter : Implements IValueConverter
Public Function Convert(value As Object, targetType As Type,
parameter As Object,
culture As System.Globalization.CultureInfo) As Object
Implements System.Windows.Data.IValueConverter.Convert
Dim imageSource As ImageSource = Nothing
Dim type = value.GetType
If type.Equals(GetType(Icon)) Then
imageSource = DirectCast(value, Icon).ToImageSource
ElseIf type.Equals(GetType(Bitmap)) Then
imageSource = DirectCast(value, Bitmap).ToImageSource
End If
Return imageSource
End Function
Public Function ConvertBack(value As Object, targetType As Type,
parameter As Object,
culture As System.Globalization.CultureInfo) As Object Implements
System.Windows.Data.IValueConverter.ConvertBack
Throw New NotSupportedException
End Function
End Class
End Namespace
Imports System.Drawing
Namespace Converters
<ValueConversion(GetType(MarshalByRefObject), GetType(BitmapSource))> _
Public Class ImageSourceConverter : Implements IValueConverter
Public Function Convert(value As Object, targetType As Type,
parameter As Object,
culture As System.Globalization.CultureInfo) As Object
Implements System.Windows.Data.IValueConverter.Convert
Dim imageSource As ImageSource = Nothing
Dim type = value.GetType
If type.Equals(GetType(Icon)) Then
imageSource = DirectCast(value, Icon).ToImageSource
ElseIf type.Equals(GetType(Bitmap)) Then
imageSource = DirectCast(value, Bitmap).ToImageSource
End If
Return imageSource
End Function
Public Function ConvertBack(value As Object, targetType As Type,
parameter As Object,
culture As System.Globalization.CultureInfo) As Object Implements
System.Windows.Data.IValueConverter.ConvertBack
Throw New NotSupportedException
End Function
End Class
End Namespace