如何附加属性(通过 .NET 基础设施或任何其他)
我有一个 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
'...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
所以我可以这样使用它:
Dim i As New Image With {.Source = My.Resources.Image.ToImageSource}
看一下 MyWpfExtensions.vb,我发现 Microsoft 很少有允许非官方编码的基础设施,这是我向你们的专家提出的问题。
我希望为 System.Drawing.Bitmap/Icon 类型的每个资源提供一个附加(或重写)属性,该属性通过 ex 返回图像。方法,这样我就不必在 Xaml 中使用转换器,而是直接使用它。
我实际上正在寻找类似 Microsoft.VisualBasic.MyGroupCollectionAttribute 的东西。
有什么想法吗?...
I have a WPF application and I added to the project resources many icons and bitmaps.
Now I can access them like this:
Dim ico As System.Drawing.Icon = My.Resources.Icon 'Icon.ico
Dim img As System.Drawing.Bitmap = My.Resources.Image 'Image.png
In order to use it in wpf I created too simple Extension Methods that convert them to ImageSource type:
'...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
So I can use it this way:
Dim i As New Image With {.Source = My.Resources.Image.ToImageSource}
Taking a look at MyWpfExtensions.vb reveals me that there are few Microsoft infrastractures that allow unofficial coding and here comes my question to the experts of you.
I'd like to have for each resource of type System.Drawing.Bitmap/Icon an additional (or overriding) property that returns the image via the ex. method so I don't have to use a converter in the Xaml, but use it directly.
I am actually looking for something like Microsoft.VisualBasic.MyGroupCollectionAttribute.
Any ideas?...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想除了转换器之外没有其他方法,所以让我们发布它:
I guess there is not other way but a converter so let's post it: