Skip to content

Commit 30d91ea

Browse files
committed
Added ICommandAttribute type
1 parent dadbd48 commit 30d91ea

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

Microsoft.Toolkit.Mvvm.SourceGenerators/Microsoft.Toolkit.Mvvm.SourceGenerators.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<Compile Include="..\Microsoft.Toolkit.Mvvm\ComponentModel\Attributes\ObservableObjectAttribute.cs" Link="Microsoft.Toolkit.Mvvm\ComponentModel\Attributes\ObservableObjectAttribute.cs" />
2020
<Compile Include="..\Microsoft.Toolkit.Mvvm\ComponentModel\Attributes\ObservablePropertyAttribute.cs" Link="Microsoft.Toolkit.Mvvm\ComponentModel\Attributes\ObservablePropertyAttribute.cs" />
2121
<Compile Include="..\Microsoft.Toolkit.Mvvm\ComponentModel\Attributes\ObservableRecipientAttribute.cs" Link="Microsoft.Toolkit.Mvvm\ComponentModel\Attributes\ObservableRecipientAttribute.cs" />
22+
<Compile Include="..\Microsoft.Toolkit.Mvvm\Input\Attributes\ICommandAttribute.cs" Link="Microsoft.Toolkit.Mvvm\Input\Attributes\ICommandAttribute.cs" />
2223
</ItemGroup>
2324

2425
<ItemGroup>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
#pragma warning disable CS1574
6+
7+
using System;
8+
using System.Windows.Input;
9+
10+
namespace Microsoft.Toolkit.Mvvm.Input
11+
{
12+
/// <summary>
13+
/// An attribute that can be used to automatically generate <see cref="ICommand"/> properties from declared methods. When this attribute
14+
/// is used to decorate a method, a generator will create a command property with the corresponding <see cref="IRelayCommand"/> interface
15+
/// depending on the signature of the method. If an invalid method signature is used, the generator will report an error.
16+
/// <para>
17+
/// In order to use this attribute, the containing type doesn't need to implement any interfaces. The generated properties will be lazily
18+
/// assigned but their value will never change, so there is no need to support property change notifications or other additional functionality.
19+
/// </para>
20+
/// <para>
21+
/// This attribute can be used as follows:
22+
/// <code>
23+
/// partial class MyViewModel
24+
/// {
25+
/// [ICommand]
26+
/// private void GreetUser(User? user)
27+
/// {
28+
/// Console.WriteLine($"Hello {user.Name}!");
29+
/// }
30+
/// }
31+
/// </code>
32+
/// And with this, code analogous to this will be generated:
33+
/// <code>
34+
/// partial class MyViewModel
35+
/// {
36+
/// private IRelayCommand? greetUserCommand;
37+
///
38+
/// public IRelayCommand GreetUserCommand => greetUserCommand ??= new RelayCommand(GreetUser);
39+
/// }
40+
/// </code>
41+
/// </para>
42+
/// <para>
43+
/// The following signatures are supported for annotated methods:
44+
/// <code>
45+
/// void Method();
46+
/// </code>
47+
/// Will generate an <see cref="IRelayCommand"/> property (using a <see cref="RelayCommand"/> instance).
48+
/// <code>
49+
/// void Method(T?);
50+
/// </code>
51+
/// Will generate an <see cref="IRelayCommand{T}"/> property (using a <see cref="RelayCommand{T}"/> instance).
52+
/// <code>
53+
/// Task Method();
54+
/// Task Method(CancellationToken);
55+
/// </code>
56+
/// Will both generate an <see cref="IAsyncRelayCommand"/> property (using an <see cref="AsyncRelayCommand{T}"/> instance).
57+
/// <code>
58+
/// Task Method(T?);
59+
/// Task Method(T?, CancellationToken);
60+
/// </code>
61+
/// Will both generate an <see cref="IAsyncRelayCommand{T}"/> property (using an <see cref="AsyncRelayCommand{T}"/> instance).
62+
/// </para>
63+
/// </summary>
64+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
65+
public sealed class ICommandAttribute : Attribute
66+
{
67+
}
68+
}

0 commit comments

Comments
 (0)