I am trying to implement a basic login page that utilizes API configuration settings (API URL, SSL option, tenant ID) set on a settings content page. I currently have the two pages linked by the navigation API, with an icon in the Navigation.TitleView navigating the user to the settings page. I am attempting to use Xamarin.Essentials Preferences to store the configuration values. I tried to follow some tutorials on creating a BaseViewModel that implements the OnPropertyChanged interface and setting up properties in my ViewModels that call that event after the value has been set. I seem to be lost on how to get the StartPage to listen for and update it's properties when a preference value is updated on the SettingsPage. Currently it only updates after the application is restarted. Below is my code for my settings model and my ViewModels for my StartPage and SettingsPage. Assume there are text fields on the XAML pages that bind to the properties in the ViewModels.
ApplicationSettings Model
using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using Xamarin.Essentials; namespace FieldServiceMobileApp.Models { public static class ApplicationSettings { public static string Hostname { get { if(!Preferences.ContainsKey("Hostname")) { Preferences.Set("Hostname", ""); } return Preferences.Get("Hostname", ""); } set => Preferences.Set("Hostname", value); } public static bool UseSSL { get { if (!Preferences.ContainsKey("UseSSL")) { Preferences.Set("UseSSL", true); } return Preferences.Get("UseSSL", true); } set => Preferences.Set("UseSSL", value); } public static string ConfigGroup { get { if (!Preferences.ContainsKey("ConfigGroup")) { Preferences.Set("ConfigGroup", ""); } return Preferences.Get("ConfigGroup", ""); } set => Preferences.Set("ConfigGroup", value); } } }
StartPage ViewModel
using FieldServiceMobileApp.Models; using FieldServiceMobileApp.Services; using System.Collections.ObjectModel; using Xamarin.Forms; namespace FieldServiceMobileApp.ViewModels { public class StartPageViewModel : BaseViewModel { public string Hostname { get => ApplicationSettings.Hostname; set { if (ApplicationSettings.Hostname == value) { return; } ApplicationSettings.Hostname = value; OnPropertyChanged("Hostname"); } } public bool UseSSL { get => ApplicationSettings.UseSSL; set { if (ApplicationSettings.UseSSL == value) { return; } ApplicationSettings.UseSSL = value; OnPropertyChanged("UseSSL"); } } public string ConfigGroup { get => ApplicationSettings.ConfigGroup; set { if (ApplicationSettings.ConfigGroup == value) { return; } ApplicationSettings.ConfigGroup = value; OnPropertyChanged("ConfigGroup"); } } // Observable collection for Mongoose Configurations: private ObservableCollection<MongooseConfiguration> _MongooseConfigurations; public ObservableCollection<MongooseConfiguration> MongooseConfigurations { get { return _MongooseConfigurations; } set { _MongooseConfigurations = value; OnPropertyChanged("MongooseConfigurations"); } } // SelectedConfiguration Property: private MongooseConfiguration _SelectedConfiguration; public MongooseConfiguration SelectedConfiguration { get => _SelectedConfiguration; set { _SelectedConfiguration = value; OnPropertyChanged("SelectedConfiguration"); } } // Username Property: private string _Username; public string Username { get => _Username; set { if (_Username == value) { return; } _Username = value; OnPropertyChanged("Username"); } } // Password Property: private string _Password; public string Password { get => _Password; set { if (_Password == value) { return; } _Password = value; OnPropertyChanged("Password"); } } public StartPageViewModel(iNavigationService FSMNavigationService) : base(FSMNavigationService) { } public override void Init() { Hostname = ApplicationSettings.Hostname; UseSSL = ApplicationSettings.UseSSL; ConfigGroup = ApplicationSettings.ConfigGroup; } public Command SettingsCommand => new Command(async () => await _FSMNavigationService.NavigateTo<SettingsViewModel>()); } }
SettingsPage ViewModel
using FieldServiceMobileApp.Models; using System; using FieldServiceMobileApp.Services; namespace FieldServiceMobileApp.ViewModels { public class SettingsViewModel : BaseViewModel { public string Hostname { get => ApplicationSettings.Hostname; set { if (ApplicationSettings.Hostname == value) { return; } ApplicationSettings.Hostname = value; OnPropertyChanged("Hostname"); } } public bool UseSSL { get => ApplicationSettings.UseSSL; set { if (ApplicationSettings.UseSSL == value) { return; } ApplicationSettings.UseSSL = value; OnPropertyChanged("UseSSL"); } } public string ConfigGroup { get => ApplicationSettings.ConfigGroup; set { if (ApplicationSettings.ConfigGroup == value) { return; } ApplicationSettings.ConfigGroup = value; OnPropertyChanged("ConfigGroup"); } } public SettingsViewModel(iNavigationService FSMNavigationService) : base(FSMNavigationService) { Hostname = ApplicationSettings.Hostname; UseSSL = ApplicationSettings.UseSSL; ConfigGroup = ApplicationSettings.ConfigGroup; } public override void Init() { } } }