From cdd4a4aa86dceb94ae87bd54653202ee96307527 Mon Sep 17 00:00:00 2001 From: lobroda Date: Sun, 15 Oct 2023 17:31:34 +0100 Subject: [PATCH] ADD : Use Command for OnSelectionChanged in TousView --- LivreLand/View/TousView.xaml | 3 ++- LivreLand/View/TousView.xaml.cs | 9 --------- LivreLand/ViewModel/TousVM.cs | 16 +++++++++++++++- ViewModels/ManagerVM.cs | 14 +++++++++++++- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/LivreLand/View/TousView.xaml b/LivreLand/View/TousView.xaml index e77b7c4..3b0188b 100644 --- a/LivreLand/View/TousView.xaml +++ b/LivreLand/View/TousView.xaml @@ -39,7 +39,8 @@ diff --git a/LivreLand/View/TousView.xaml.cs b/LivreLand/View/TousView.xaml.cs index 8cfdfb8..056f607 100644 --- a/LivreLand/View/TousView.xaml.cs +++ b/LivreLand/View/TousView.xaml.cs @@ -26,14 +26,5 @@ public partial class TousView : ContentPage #region Methods - void OnSelectionChanged(object sender, SelectionChangedEventArgs e) - { - if (e.CurrentSelection.FirstOrDefault() is BookVM) - { - var result = new DetailsLivreVM(TousVM.Manager, TousVM.Navigator, e.CurrentSelection.FirstOrDefault() as BookVM); - App.Current.MainPage.Navigation.PushAsync(new DetailsLivreView(result)); - } - } - #endregion } \ No newline at end of file diff --git a/LivreLand/ViewModel/TousVM.cs b/LivreLand/ViewModel/TousVM.cs index 9724dac..9e995ab 100644 --- a/LivreLand/ViewModel/TousVM.cs +++ b/LivreLand/ViewModel/TousVM.cs @@ -1,9 +1,11 @@ -using PersonalMVVMToolkit; +using LivreLand.View; +using PersonalMVVMToolkit; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Windows.Input; using ViewModels; namespace LivreLand.ViewModel @@ -16,6 +18,7 @@ namespace LivreLand.ViewModel public ManagerVM Manager { get; private set; } + public ICommand OnSelectionChangedCommand { get; private set; } #endregion #region Constructor @@ -24,6 +27,17 @@ namespace LivreLand.ViewModel { Navigator = navigatorVM; Manager = managerVM; + OnSelectionChangedCommand = new RelayCommand((bookVM) => OnSelectionChanged(bookVM)); + } + + #endregion + + #region Methods + + private void OnSelectionChanged(BookVM bookVM) + { + var result = new DetailsLivreVM(Manager, Navigator, bookVM); + App.Current.MainPage.Navigation.PushAsync(new DetailsLivreView(result)); } #endregion diff --git a/ViewModels/ManagerVM.cs b/ViewModels/ManagerVM.cs index 11f4c46..5c623f6 100644 --- a/ViewModels/ManagerVM.cs +++ b/ViewModels/ManagerVM.cs @@ -27,6 +27,7 @@ namespace ViewModels private int index; private long nbBooks; + private BookVM selectedBook; private AuthorVM selectedAuthor; private PublishDateVM selectedDate; private RatingsVM selectedRating; @@ -99,7 +100,18 @@ namespace ViewModels get => status; } - public BookVM SelectedBook { get; set; } + public BookVM SelectedBook + { + get { return selectedBook; } + set + { + if (selectedBook != value) + { + selectedBook = value; + OnPropertyChanged(nameof(SelectedBook)); + } + } + } public AuthorVM SelectedAuthor {