简体   繁体   English

WP7中的扩展

[英]Extension in WP7

Imports System.Runtime.CompilerServices

Public Module ColorExtension
    <Extension()> _
    Public Function ToColor(ByVal argb As UInteger) As Global.System.Windows.Media.Color
        Return Global.System.Windows.Media.Color.FromArgb(CByte((argb And -16777216) >> &H18), CByte((argb And &HFF0000) >> &H10), CByte((argb And &HFF00) >> 8), CByte(argb And &HFF))
    End Function
End Module



Public Class Test
    Private Sub TestExt()
        Dim Col As System.Windows.Media.Color
        Col = System.Windows.Media.Color.ToColor(100)'<-- Error
        Col.ToColor(100)'<-- Error
    End Sub
End Class

When I use this code, I got this exception Error 1 'ToColor' is not a member of 'System.Windows.Media.Color'. 当我使用此代码时,出现此异常错误1'ToColor'不是'System.Windows.Media.Color'的成员。 C:...\\ColorExtension.vb C:... \\ ColorExtension.vb

Please any advice how to develop Extension functions like this Color extension one. 请任何建议如何开发扩展功能,例如此颜色扩展之一。

  1. Extension methods can be fired on object of class, not the class itself. 扩展方法可以在类的对象上触发,而不是在类本身上触发。
  2. Your extension method is set on UInteger class, not on Color . 您的扩展方法是在UInteger类上设置的,而不是在Color Type you extends is the type of first method parameter. 您扩展的类型是第一个方法参数的类型。
  3. You cannot add new method to a class that could be fired like you tried to do it. 您无法将新方法添加到可能像您尝试执行的那样触发的类。

You can use your extension method in two ways: 您可以通过两种方式使用扩展方法:

  1. Standard method invocation: ColorExtension.ToColor(100) 标准方法调用: ColorExtension.ToColor(100)
  2. Using the extension method syntax: 100.ToColor() 使用扩展方法语法: 100.ToColor()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM