PageRenderTime 32ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-toolkit/PhoneToolkitSample/Extensions.vb

https://bitbucket.org/jeremejevs/milk-manager
Visual Basic | 148 lines | 96 code | 34 blank | 18 comment | 0 complexity | 0dcd838a9cac8035bc42d854bb506e3f MD5 | raw file
  1. ' (c) Copyright Microsoft Corporation.
  2. ' This source is subject to the Microsoft Public License (Ms-PL).
  3. ' Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
  4. ' All other rights reserved.
  5. Imports System.Linq
  6. Public Module Extensions
  7. ''' <summary>
  8. ''' Return a random item from a list.
  9. ''' </summary>
  10. ''' <typeparam name="T">The item type.</typeparam>
  11. ''' <param name="rnd">The Random instance.</param>
  12. ''' <param name="list">The list to choose from.</param>
  13. ''' <returns>A randomly selected item from the list.</returns>
  14. <Runtime.CompilerServices.Extension()>
  15. Public Function [Next](Of T)(ByVal rnd As Random, ByVal list As IList(Of T)) As T
  16. Return list(rnd.Next(list.Count))
  17. End Function
  18. End Module
  19. ''' <summary>
  20. ''' A class used to expose the Key property on a dynamically-created Linq grouping.
  21. ''' The grouping will be generated as an internal class, so the Key property will not
  22. ''' otherwise be available to databind.
  23. ''' </summary>
  24. ''' <typeparam name="TKey">The type of the key.</typeparam>
  25. ''' <typeparam name="TElement">The type of the items.</typeparam>
  26. Public Class PublicGrouping(Of TKey, TElement)
  27. Implements Linq.IGrouping(Of TKey, TElement)
  28. Private ReadOnly _internalGrouping As Linq.IGrouping(Of TKey, TElement)
  29. Public Sub New(ByVal internalGrouping As Linq.IGrouping(Of TKey, TElement))
  30. _internalGrouping = internalGrouping
  31. End Sub
  32. Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean
  33. Dim that = TryCast(obj, PublicGrouping(Of TKey, TElement))
  34. Return (that IsNot Nothing) AndAlso (Me.Key.Equals(that.Key))
  35. End Function
  36. Public Overrides Function GetHashCode() As Integer
  37. Return Key.GetHashCode()
  38. End Function
  39. #Region "IGrouping<TKey,TElement> Members"
  40. Public ReadOnly Property Key As TKey Implements Linq.IGrouping(Of TKey, TElement).Key
  41. Get
  42. Return _internalGrouping.Key
  43. End Get
  44. End Property
  45. #End Region
  46. #Region "IEnumerable<TElement> Members"
  47. Public Function GetEnumerator() As IEnumerator(Of TElement) Implements Collections.Generic.IEnumerable(Of TElement).GetEnumerator
  48. Return _internalGrouping.GetEnumerator()
  49. End Function
  50. #End Region
  51. #Region "IEnumerable Members"
  52. Private Function IEnumerable_GetEnumerator() As Collections.IEnumerator Implements Collections.IEnumerable.GetEnumerator
  53. Return _internalGrouping.GetEnumerator()
  54. End Function
  55. #End Region
  56. End Class
  57. Public Class CommandButton
  58. Inherits Button
  59. Public Property Command As ICommand
  60. Get
  61. Return CType(GetValue(CommandProperty), ICommand)
  62. End Get
  63. Set(ByVal value As ICommand)
  64. SetValue(CommandProperty, value)
  65. End Set
  66. End Property
  67. Public Shared ReadOnly CommandProperty As DependencyProperty =
  68. DependencyProperty.Register("Command", GetType(ICommand), GetType(CommandButton),
  69. New PropertyMetadata(AddressOf OnCommandChanged))
  70. Private Shared Sub OnCommandChanged(ByVal obj As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
  71. CType(obj, CommandButton).OnCommandChanged(e)
  72. End Sub
  73. Public Property CommandParameter As Object
  74. Get
  75. Return CObj(GetValue(CommandParameterProperty))
  76. End Get
  77. Set(ByVal value As Object)
  78. SetValue(CommandParameterProperty, value)
  79. End Set
  80. End Property
  81. Public Shared ReadOnly CommandParameterProperty As DependencyProperty =
  82. DependencyProperty.Register("CommandParameter", GetType(Object), GetType(CommandButton),
  83. New PropertyMetadata(AddressOf OnCommandParameterChanged))
  84. Private Shared Sub OnCommandParameterChanged(ByVal obj As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
  85. CType(obj, CommandButton).UpdateIsEnabled()
  86. End Sub
  87. Private Sub OnCommandChanged(ByVal e As DependencyPropertyChangedEventArgs)
  88. If e.OldValue IsNot Nothing Then
  89. Dim command = TryCast(e.OldValue, ICommand)
  90. If command IsNot Nothing Then
  91. RemoveHandler command.CanExecuteChanged, AddressOf CommandCanExecuteChanged
  92. End If
  93. End If
  94. If e.NewValue IsNot Nothing Then
  95. Dim command = TryCast(e.NewValue, ICommand)
  96. If command IsNot Nothing Then
  97. AddHandler command.CanExecuteChanged, AddressOf CommandCanExecuteChanged
  98. End If
  99. End If
  100. UpdateIsEnabled()
  101. End Sub
  102. Private Sub CommandCanExecuteChanged(ByVal sender As Object, ByVal e As EventArgs)
  103. UpdateIsEnabled()
  104. End Sub
  105. Private Sub UpdateIsEnabled()
  106. IsEnabled = If(Command IsNot Nothing, Command.CanExecute(CommandParameter), False)
  107. End Sub
  108. Protected Overrides Sub OnClick()
  109. MyBase.OnClick()
  110. If Command IsNot Nothing Then
  111. Command.Execute(CommandParameter)
  112. End If
  113. End Sub
  114. End Class