Chuyển đến nội dung
Skillpedia
API và máy chủ (backend)40k · #7304

mvvm-toolkit

Skill này làm gì

Công cụ này giúp lập trình viên viết mã cho ứng dụng hiệu quả hơn. Nó tự động hóa những phần lặp đi lặp lại khi tạo giao diện người dùng và lệnh, giúp phát triển nhanh hơn và ít lỗi hơn.

Khi nào nên dùng

  • ✓Khi bạn tạo hoặc rà soát logic hiển thị (ViewModel) trong ứng dụng.
  • ✓Khi bạn cần định nghĩa các nút hoặc hành động mà người dùng có thể thực hiện.
  • ✓Khi bạn kiểm tra thông tin người dùng nhập vào biểu mẫu.

Liên kết skill

https://github.com/github/awesome-copilot/blob/main/skills/mvvm-toolkit/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/github/awesome-copilot/blob/main/skills/mvvm-toolkit/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Làm thế nào để tạo một nút lưu dữ liệu người dùng và chỉ hoạt động khi ô tên không để trống?”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Công cụ dùng các 'code generators' tự động tạo thêm mã dựa trên các chú thích đơn giản mà lập trình viên thêm vào. Điều này giúp lập trình viên không phải viết tay quá nhiều mã lặp lại.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

CommunityToolkit.Mvvm (core)

Use this skill when authoring or reviewing ViewModels, properties, commands, or validation in apps that use CommunityToolkit.Mvvm 8.x.

Companion skills. Load mvvm-toolkit-messenger for IMessenger pub/sub patterns. Load mvvm-toolkit-di for Microsoft.Extensions.DependencyInjection integration.

Quick recap. [ObservableProperty] on private fields in partial classes; [RelayCommand] on instance methods; inherit from ObservableObject (or ObservableValidator for input forms, ObservableRecipient when using IMessenger).


Package & setup

<ItemGroup>
  <PackageReference Include="CommunityToolkit.Mvvm" Version="8.*" />
</ItemGroup>

Targets: netstandard2.0, netstandard2.1, net6.0+. Works on .NET, .NET Framework, Mono. Source generators ship in the same NuGet — no extra analyzer reference required.

Namespaces:

using CommunityToolkit.Mvvm.ComponentModel;   // ObservableObject, [ObservableProperty]
using CommunityToolkit.Mvvm.Input;             // [RelayCommand], RelayCommand, AsyncRelayCommand

Universal rule. Every type that uses [ObservableProperty] or [RelayCommand] — and every enclosing type, if nested — must be declared partial. Without it, the generators emit MVVMTK0008 / MVVMTK0042.


Source generators cheat sheet

Attribute Applied to Generates
[ObservableProperty] private field Public INotifyPropertyChanged property + OnXxxChanging/OnXxxChanged partial-method hooks
[NotifyPropertyChangedFor(nameof(Other))] observable field Also raises PropertyChanged for the listed property
[NotifyCanExecuteChangedFor(nameof(MyCommand))] observable field Calls MyCommand.NotifyCanExecuteChanged() on change
[NotifyDataErrorInfo] observable field on ObservableValidator Calls ValidateProperty(value) from the setter
[NotifyPropertyChangedRecipients] observable field on ObservableRecipient Broadcast(old, new) after the change
[RelayCommand] instance method Lazy RelayCommand / AsyncRelayCommand exposed as IRelayCommand / IAsyncRelayCommand
[RelayCommand(CanExecute = nameof(CanX))] instance method Wires CanExecute to a method or property
[RelayCommand(IncludeCancelCommand = true)] async method with CancellationToken Also generates XxxCancelCommand
[RelayCommand(AllowConcurrentExecutions = true)] async method Allows queued/parallel invocations (default disables while running)
[RelayCommand(FlowExceptionsToTaskScheduler = true)] async method Surfaces exceptions via ExecutionTask instead of awaiting and rethrowing
[property: SomeAttr] observable field or [RelayCommand] method Forwards SomeAttr onto the generated property (e.g., [JsonIgnore])

Naming. Field name / _name / m_name → Name. Method LoadAsync → LoadCommand (the Async suffix is stripped; a leading On is also stripped).

See references/source-generators.md for the full attribute reference with generated-code samples.


ViewModel patterns

Simple observable property

public partial class ContactViewModel : ObservableObject
{
    [ObservableProperty]
    private string? name;
}

Hooks: OnXxxChanging / OnXxxChanged

[ObservableProperty]
private string? name;

partial void OnNameChanged(string? value) =>
    Logger.LogInformation("Name changed to {Name}", value);

Both single-arg (value) and two-arg (oldValue, newValue) overloads are available. Implement only the ones you need; unimplemented hooks are elided by the compiler (zero runtime cost).

Dependent properties + dependent commands

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(FullName))]
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
private string? firstName;

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(FullName))]
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
private string? lastName;

public string FullName => $"{FirstName} {LastName}".Trim();

Wrapping a non-observable model

public sealed class ObservableUser(User user) : ObservableObject
{
    public string Name
    {
        get => user.Name;
        set => SetProperty(user.Name, value, user, (u, n) => u.Name = n);
    }
}

…

Câu hỏi thường gặp

Tôi có cần cài thêm gì không?

Không, công cụ chính đi kèm khi bạn cài gói `CommunityToolkit.Mvvm`.

“Code generators” là gì?

Đó là các công cụ tự động viết mã cho bạn dựa trên hướng dẫn bạn cung cấp. Chúng giúp tránh viết mã lặp lại.

Khi nào nên dùng `ObservableValidator` thay vì `ObservableObject`?

Dùng `ObservableValidator` khi ứng dụng cần kiểm tra thông tin người dùng nhập vào biểu mẫu, chẳng hạn như kiểm tra một trường bắt buộc đã được điền chưa.

Chi tiết

Mô tả gốc của tác giả

CommunityToolkit.Mvvm (the MVVM Toolkit) core: source generators ([ObservableProperty], [RelayCommand], [NotifyPropertyChangedFor], [NotifyCanExecuteChangedFor], [NotifyDataErrorInfo]), base classes (ObservableObject / ObservableValidator / ObservableRecipient), commands (RelayCommand / AsyncRelayCommand), and validation. Companion skills: mvvm-toolkit-messenger for pub/sub, mvvm-toolkit-di for Mi

repo: github/awesome-copilot

Độ phổ biến

Sao GitHub
40k
Vị trí xếp hạng
#7304
Giấy phép
MIT
Cập nhật
15/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự