Hi have a conception problem. I work on a pro app to calculate some scores. I do it with C# / Xamarin.Forms. I want to manage favorites, so that the user can have a limited score list to find its favorites faster.
I have 4 tabs :
- Entire score list ==> navigates to chosen score
- Favorites list ==> navigates too
- & 4. : not a problem here
So I want that when the user adds/deletes a score from the favorites list, this is changed in the first and the second tab. For the moment I have this :
public static class FavoritesManager
{
public static ObservableCollection<string> FavoritesList = new ObservableCollection<string>();
// Indexer does not work because static class ==> this is one of the problems
// public bool this[string key] { get => this.Favs.Contains(key); }
}
// My ViewModel
public class ScoreListViewModel : ViewModelBase
{
// Each Category is a List<Score>. Score has 3 properties : string Title, string Detail, bool IsFavorite
public ObservableCollection<Category> Categories { get; set; }
public ScoreListViewModel()
{
this.InitializeCategories();
FavoritesManager.FavoritesList.CollectionChanged += OnFavoritesChanged;
}
// When favorites list has changed ==> event CollectionChanged
public void OnFavoritesChanged(object sender, NotifyCollectionChangedEventArgs e)
{
this.InitializeCategories();
}
public void InitializeCategories()
{
this.Categories = new ObservableCollection<Category>
{
new Category ("Cat1")
{
new Score("Foo", "Bar", FavoritesManager.FavoritesList.Contains("Foo"))
}
};
}
// Command used to add a favorite
public ICommand AddToFavorites => new Command<string>((fav) =>
{
FavoritesManager.FavoritesList.Add(fav);
});
}
So I have 2 questions :
-
How to avoid dependency of ViewModel to the static class FavoritesManager ? Do I really need a static class or is there another way to "share" it in real time through different views ? Because if I decide to change favorites management, when I will have 30-40 scores in the list, it will be very difficult...
-
Is there a way to avoid complete reinitialization of the Categories list each time I change just 1 thing (1 favorite) ? This is, I think, mostly a XAML / Binding question...
Thanks for your help, Galactose
Aucun commentaire:
Enregistrer un commentaire