ycliper

Популярное

Музыка Кино и Анимация Автомобили Животные Спорт Путешествия Игры Юмор

Интересные видео

2025 Сериалы Трейлеры Новости Как сделать Видеоуроки Diy своими руками

Топ запросов

смотреть а4 schoolboy runaway турецкий сериал смотреть мультфильмы эдисон
Скачать

ICommand and Commands in .NET MAUI — Button Logic That Belongs in the ViewModel in C# | Metrik Rule

Автор: Metrik Rule

Загружено: 2026-06-04

Просмотров: 10

Описание: With the ViewModel holding the data and notifying the UI of changes, there is still one piece out of place: button logic. Every tap handler lives in the code-behind — the page file — casting BindingContext to find the ViewModel and calling a method. That puts business decisions in the UI layer, makes the ViewModel impossible to test without instantiating a real page, and creates a particularly painful problem for buttons inside a DataTemplate, where CommandParameter casts add fragile type dependencies to the page.

ICommand, from System.Windows.Input, is the standard contract for wrapping an action so it can be bound directly in XAML. It exposes three members: Execute runs the action, CanExecute returns whether the button should be enabled, and CanExecuteChanged is raised when the enabled state needs to be re-evaluated. MAUI's concrete Command class implements ICommand without requiring a custom implementation. The video walks through each constructor form — Command(Action), Command(Action, Func) for enable/disable logic, Command(Action) for typed parameters, and Command(Action, Func) — and shows how to declare command properties as ICommand (get-only, initialized in the ViewModel constructor), replace Clicked= with Command="{Binding CommandName}" in XAML, and call ChangeCanExecute on the Command to re-run the canExecute function and automatically gray or re-enable the button. For buttons inside a DataTemplate, the BindingContext is the item, not the ViewModel, so a plain {Binding CommandName} finds nothing. The fix is x:Name on the ContentPage and a Source={x:Reference PageName}, Path=BindingContext.CommandName binding — two different binding sources on the same button tap, one for the command and one for CommandParameter.

This is part of the Computer Science I with C# series on the Metrik Rule channel.

🎵 Sound effects and background music: https://nomadicsound.org

Не удается загрузить Youtube-плеер. Проверьте блокировку Youtube в вашей сети.
Повторяем попытку...
ICommand and Commands in .NET MAUI — Button Logic That Belongs in the ViewModel in C# | Metrik Rule

Поделиться в:

Доступные форматы для скачивания:

Скачать видео

  • Информация по загрузке:

Скачать аудио

Похожие видео

© 2025 ycliper. Все права защищены.



  • Контакты
  • О нас
  • Политика конфиденциальности



Контакты для правообладателей: [email protected]