1 | #Region "Microsoft.VisualBasic::3c7df8d3ef260417cfe265b34c962fba, Microsoft.VisualBasic.Core\ComponentModel\RelayCommand.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Class ICallbackInvoke |
35 | ' |
36 | ' Properties: CallbackInvoke |
37 | ' |
38 | ' Constructor: (+1 Overloads) Sub New |
39 | ' Function: ToString |
40 | ' |
41 | ' Class RelayCommand |
42 | ' |
43 | ' Constructor: (+2 Overloads) Sub New |
44 | ' |
45 | ' Function: CanExecute |
46 | ' |
47 | ' Sub: Execute |
48 | ' |
49 | ' |
50 | ' /********************************************************************************/ |
51 | |
52 | #End Region |
53 | |
54 | Imports System.Runtime.CompilerServices |
55 | Imports System.Windows.Input |
56 | Imports Microsoft.VisualBasic.Parallel.Tasks |
57 | |
58 | Namespace ComponentModel |
59 | |
60 | Public MustInherit Class ICallbackInvoke |
61 | Implements ICallbackTask |
62 | |
63 | Protected ReadOnly _execute As Action |
64 | |
65 | Public ReadOnly Property CallbackInvoke As Action Implements ICallbackTask.CallbackInvoke |
66 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
67 | Get |
68 | Return _execute |
69 | End Get |
70 | End Property |
71 | |
72 | Const execute As String = NameOf(execute) |
73 | |
74 | Protected Sub New(callback As Action) |
75 | If callback Is Nothing Then |
76 | Throw New ArgumentNullException(execute) |
77 | Else |
78 | _execute = callback |
79 | End If |
80 | End Sub |
81 | |
82 | Public Overrides Function ToString() As String |
83 | Return _execute.ToString |
84 | End Function |
85 | End Class |
86 | |
87 | ''' <summary> |
88 | ''' Taken from http://msdn.microsoft.com/en-us/magazine/dd419663.aspx |
89 | ''' </summary> |
90 | Public Class RelayCommand : Inherits ICallbackInvoke |
91 | Implements ICommand |
92 | |
93 | #Region "Members" |
94 | |
95 | ReadOnly _canExecute As Func(Of [Boolean]) |
96 | |
97 | #End Region |
98 | |
99 | #Region "Constructors" |
100 | |
101 | Public Sub New(execute As Action) |
102 | Me.New(execute, Nothing) |
103 | End Sub |
104 | |
105 | Public Sub New(execute As Action, canExecute As Func(Of [Boolean])) |
106 | Call MyBase.New(execute) |
107 | _canExecute = canExecute |
108 | End Sub |
109 | |
110 | #End Region |
111 | |
112 | #Region "ICommand Members" |
113 | |
114 | <DebuggerStepThrough> |
115 | Public Function CanExecute(parameter As [Object]) As [Boolean] Implements ICommand.CanExecute |
116 | Return If(_canExecute Is Nothing, True, _canExecute()) |
117 | End Function |
118 | |
119 | Public Sub Execute(parameter As [Object]) Implements ICommand.Execute |
120 | _execute() |
121 | End Sub |
122 | |
123 | #End Region |
124 | |
125 | Public Event CanExecuteChanged(sender As Object, e As EventArgs) Implements ICommand.CanExecuteChanged |
126 | End Class |
127 | End Namespace |