Created
January 26, 2016 06:22
-
-
Save dkudelko/778ec86b17ff8117815f to your computer and use it in GitHub Desktop.
ItemList MVVM Tap Command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Windows.Input; | |
| using Xamarin.Forms; | |
| namespace YourNS { | |
| public class ListView : Xamarin.Forms.ListView { | |
| public static BindableProperty ItemClickCommandProperty = BindableProperty.Create<ListView, ICommand>(x => x.ItemClickCommand, null); | |
| public ListView() { | |
| this.ItemTapped += this.OnItemTapped; | |
| } | |
| public ICommand ItemClickCommand { | |
| get { return (ICommand)this.GetValue(ItemClickCommandProperty); } | |
| set { this.SetValue(ItemClickCommandProperty, value); } | |
| } | |
| private void OnItemTapped(object sender, ItemTappedEventArgs e) { | |
| if (e.Item != null && this.ItemClickCommand != null && this.ItemClickCommand.CanExecute(e)) { | |
| this.ItemClickCommand.Execute(e.Item); | |
| this.SelectedItem = null; | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ContentPage ... | |
| xmlns:local="clr-namespace:Samples.Views;assembly=Your Assebly Name"> | |
| <local:ListView ItemClickCommand="{Binding TapCommand}" | |
| ItemsSource="{Binding List}"> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private Command<Signature> _tapCommand; | |
| public Command<Signature> TapCommand { | |
| get { | |
| this._tapCommand = this._tapCommand ?? new Command<Signature>(s => | |
| this.dialogs.ActionSheet(new ActionSheetConfig() | |
| .Add("View", () => { | |
| if (!this.fileViewer.Open(s.FilePath)) | |
| this.dialogs.Alert(String.Format("Could not open file {0}", s.FileName)); | |
| }) | |
| .Add("Cancel") | |
| ) | |
| ); | |
| return this._tapCommand; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public ViewModelCase1() | |
| { | |
| ItemList = new List<SampleModel>().ToObservableCollection(); | |
| TapCommand = new Command((e) => | |
| { | |
| var itemselected = e as SampleModel; | |
| Debug.WriteLine(e); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment