Hi,
I had some trouble with a piece of code which was not working (my bad), and I restored it successfully. This code uses Preferences from Xamarin Essentials, and as I thought the problem was with it, I updated it with NuGet in the whole solution.
But now my problem is fixed, and I still face an issue that I can't resolve... The code works because I made tests, but I am not able to retrieve preferences at startup. Yet, preferences are correctly set at runtime, I checked its value in the app !
So, is it a known issue with Preferences ? It does not work in debug mode, neither release mode...
Precision : as I changed this piece of code to make it smarter and to preserve MVVM, I already tested Preferences and it worked very well even in debug mode... But as I updated it before being able to build again, I can't compare...
Thanks,
Galactose
(In case it's not a known issue, here is my code:
using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Linq; using Xamarin.Essentials; using CardioCALC.Models; namespace CardioCALC.ViewModels { public sealed class FavoritesViewModel : ViewModelBase { /* * SINGLETON : * - Instance * - Constructors to avoir other instance and be thread-safe */ public static FavoritesViewModel Instance { get; } = new FavoritesViewModel(); static FavoritesViewModel() { } private FavoritesViewModel() { this.Favorites.CollectionChanged += OnFavoritesSaveChanges; // Autosave } /* * CLASS : classic properties and methodes to be used in the singleton instance * - separator : constant char for serialization/deserialization * - favoritePreferenceKey : constant string to be used as Key in Xamarin.Essentials.Preferences * - Methods to Add/Remove/Toggle favorites (update list AND the score instance) */ private const char separator = ','; private const string favoritePreferenceKey = "favorites"; public ObservableCollection<Score> Favorites { get; private set; } = new ObservableCollection<Score>(); public ObservableCollection<Score> Scores { get => ScoreListViewModel.Instance.Scores; } public void ToggleFavorite(Score score) { if (score.IsFavorite) this.RemoveFavoriteAndUpdate(score); else this.AddFavoriteAndUpdate(score); } public void AddFavoriteAndUpdate(Score score) { score.IsFavorite = true; if (!this.Favorites.Contains(score)) this.Favorites.AddSorted(score); } public void RemoveFavoriteAndUpdate(Score favorite) { favorite.IsFavorite = false; this.Favorites.Remove(favorite); } // Auto-save favorites list with event private void OnFavoritesSaveChanges(object sender, NotifyCollectionChangedEventArgs e) { this.SaveFavorites(); } /* * Methods to Save/Load favorites from database/preferences(with serialization/deserialization) */ public void LoadSavedFavorites() { this.Favorites.Clear(); // Clear favorites (ie. to reload favorites after locale change in ScoreListViewModel) string favorites = Preferences.Get(favoritePreferenceKey, ""); if (favorites == "") return; foreach (Score favorite in this.Deserialize(favorites)) { this.AddFavoriteAndUpdate(favorite); } } public void SaveFavorites() { if (this.Favorites.Count > 0) Preferences.Set(favoritePreferenceKey, this.Serialize()); else Preferences.Remove(favoritePreferenceKey); } // Serialize favorites list into string format public string Serialize() { if (this.Favorites.Count < 1) return ""; string favorites = ""; foreach (Score favorite in this.Favorites) { favorites += $"{favorite.PageName}{separator}"; } return favorites.TrimEnd(separator); } // Deserialize favorites list from string to restore Collection public ObservableCollection<Score> Deserialize(string favorites) { ObservableCollection<Score> favoritesList = new ObservableCollection<Score>(); foreach(string favorite in favorites.Split(separator)) { favoritesList.Add(this.Scores.FirstOrDefault(s => s.PageName == favorite)); // this.Scores contains all scores } return favoritesList; } } }