https://github.com/enusbaum/xamarinexamples.forms.eventtocommand
Xamarin.Forms example of how to have a Xamarin.Forms element Event trigger an MVVM Command using a custom Behavior
https://github.com/enusbaum/xamarinexamples.forms.eventtocommand
Last synced: 3 months ago
JSON representation
Xamarin.Forms example of how to have a Xamarin.Forms element Event trigger an MVVM Command using a custom Behavior
- Host: GitHub
- URL: https://github.com/enusbaum/xamarinexamples.forms.eventtocommand
- Owner: enusbaum
- License: mit
- Created: 2018-02-20T19:36:57.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-02-20T21:32:43.000Z (over 7 years ago)
- Last Synced: 2025-02-07T08:48:42.981Z (5 months ago)
- Language: C#
- Size: 17.6 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# XamarinExamples.Forms.EventToCommand
This repository holds an a working example of the Xamarin Reusable EventToCommand custom Behavior which lets you bind Commands to Events for Xamarin.Form elements that do not properly support the MVVM Command Pattern.
While this example uses a [ListView](https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/) [ItemTapped](https://developer.xamarin.com/api/event/Xamarin.Forms.ListView.ItemTapped/) Event as the example, it can be applied to any Event using the appropriate Converter for the EventArgs.
Cheers!
# How it works
### Xamarin.FormsIn the Xamarin.Forms project we implement the resusable EventToCommand Behavior example as described over at the Xamarin documentation ([link](https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/behaviors/reusable/event-to-command-behavior/))
```xml
...
```
This tells Xamarin.Forms to use our EventToCommand behavior to bind the **ItemTapped** Event to the specified command **ItemTappedCommand** which is defined in the ViewModel
```csharp
public EventToCommandPageViewModel()
{
//Assign method to be invoked by Command
ItemTappedCommand = new Command((o) => ItemTapped(o));
}///
/// Command that will be mapped to the ItemTapped event
///
/// The item tapped command.
public Command ItemTappedCommand { get; }... more code ...
///
/// Handle ItemTapped Event (via Command)
///
/// Value.
void ItemTapped(object value)
{
SelectedValue = value as string ?? string.Empty;;
}
```Using this method keeps our Code Behind clean from any Event mapping and allows us to follow a pure MVVM model where Events are bound to Commands and there's loose couping between our XAML and our ViewModel.
#### FYI
* This Example makes use of James Montemagno's OnPropertyChanged() implementation of [INotifyPropertyChanged](https://developer.xamarin.com/api/type/System.ComponentModel.INotifyPropertyChanged/) which is really fantasic and should be in the base class for any MVVM ViewModel you're working with. Check out his MVVM Blog Post and video from The Xamarin Show [here](https://blog.xamarin.com/the-xamarin-show-getting-started-with-mvvm/)
```csharp
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string name = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
```* We use a little tick to keep the ListView from rendering blank items past the end of our enumerated strings. By placing the following code as the "footer" for the ListView, it'll cause it to truncate the rendering of the items at the correct point.
```xml
```
**Cheers!**
