{"id":22920906,"url":"https://github.com/enusbaum/xamarinexamples.forms.eventtocommand","last_synced_at":"2025-04-01T13:42:58.354Z","repository":{"id":88397004,"uuid":"122246302","full_name":"enusbaum/XamarinExamples.Forms.EventToCommand","owner":"enusbaum","description":"Xamarin.Forms example of how to have a Xamarin.Forms element Event trigger an MVVM Command using a custom Behavior","archived":false,"fork":false,"pushed_at":"2018-02-20T21:32:43.000Z","size":18,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-07T08:48:42.981Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/enusbaum.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-20T19:36:57.000Z","updated_at":"2022-02-16T09:03:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"8cabf73e-60bf-475b-a075-b63bc617d4c3","html_url":"https://github.com/enusbaum/XamarinExamples.Forms.EventToCommand","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enusbaum%2FXamarinExamples.Forms.EventToCommand","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enusbaum%2FXamarinExamples.Forms.EventToCommand/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enusbaum%2FXamarinExamples.Forms.EventToCommand/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enusbaum%2FXamarinExamples.Forms.EventToCommand/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/enusbaum","download_url":"https://codeload.github.com/enusbaum/XamarinExamples.Forms.EventToCommand/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246651240,"owners_count":20811990,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-14T07:17:23.198Z","updated_at":"2025-04-01T13:42:58.340Z","avatar_url":"https://github.com/enusbaum.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# XamarinExamples.Forms.EventToCommand\n\nThis 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.\n\nWhile 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.\n\nCheers!\n# How it works\n### Xamarin.Forms\n\nIn 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/))\n\n```xml\n\u003cListView ItemsSource=\"{Binding ListValues}\"\u003e\n    ...\n    \u003cListView.Behaviors\u003e\n        \u003cb:EventToCommandBehavior EventName=\"ItemTapped\" Command=\"{Binding ItemTappedCommand}\" Converter=\"{StaticResource SelectedItemConverter}\" /\u003e\n    \u003c/ListView.Behaviors\u003e\n\u003c/ListView\u003e\n```\n\nThis tells Xamarin.Forms to use our EventToCommand behavior to bind the **ItemTapped** Event to the specified command **ItemTappedCommand** which is defined in the ViewModel\n\n```csharp\npublic EventToCommandPageViewModel()\n{\n    //Assign method to be invoked by Command\n    ItemTappedCommand = new Command((o) =\u003e ItemTapped(o));\n}\n\n/// \u003csummary\u003e\n///     Command that will be mapped to the ItemTapped event\n/// \u003c/summary\u003e\n/// \u003cvalue\u003eThe item tapped command.\u003c/value\u003e\npublic Command ItemTappedCommand { get; }\n\n... more code ...\n\n/// \u003csummary\u003e\n///     Handle ItemTapped Event (via Command)\n/// \u003c/summary\u003e\n/// \u003cparam name=\"value\"\u003eValue.\u003c/param\u003e\nvoid ItemTapped(object value)\n{\n    SelectedValue = value as string ?? string.Empty;;\n}\n```\n\nUsing 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.\n\n#### FYI\n\n* 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/)\n\n```csharp\npublic event PropertyChangedEventHandler PropertyChanged;\nvoid OnPropertyChanged([CallerMemberName] string name = \"\")\n{\n    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));\n}\n```\n\n* 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.\n```xml\n\u003c!-- This actually keeps the list from rendering blank cells past the last item--\u003e\n\u003cListView.Footer\u003e\n    \u003cStackLayout Orientation=\"Horizontal\" /\u003e\n\u003c/ListView.Footer\u003e\n```\n\n**Cheers!**\n\n![Example of ItemTapped Event through MVVM](https://d2ffutrenqvap3.cloudfront.net/items/3M2E0L3e2d3d1i0E2T0q/Screen%20Recording%202018-02-20%20at%2004.15%20PM.gif \"Example of ItemTapped Event\")\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenusbaum%2Fxamarinexamples.forms.eventtocommand","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fenusbaum%2Fxamarinexamples.forms.eventtocommand","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenusbaum%2Fxamarinexamples.forms.eventtocommand/lists"}