Monday, May 6, 2019

How to store user settings? Using xam.plugins.setting - Xamarin forms

Using a plugin from Nuget


  1. Right click on your project-click add Nuget Packages.  Type xam.plugins.setting on search. Click on it and Add package.
  2. Add it on your Android project files and IOS project files perform step 1.
  3. Make a folder called Helpers(or any folder name you want some uses Util- on the solution project then -  Create a new file called Settings.cs (empty class file)
  4. Copy and paste from readme.txt 
// Helpers/Settings.cs
using Plugin.Settings;
using Plugin.Settings.Abstractions;

//$rootnamespace$ is the name of your project followed by where the file is stored
// in this case it's in Helpers folder namespace $rootnamespace$.Helpers
{
  /// <summary>
  /// This is the Settings static class that can be used in your Core solution or in any
  /// of your client applications. All settings are laid out the same exact way with getters
  /// and setters. 
  /// </summary>

  public static class Settings
  {
    private static ISettings AppSettings
    {
      get
      {
        return CrossSettings.Current;
      }
    }

    #region Setting Constants

//variable naming
//"SettingsKey" is always a string that is a unique ID that will be stored in the device to //access constructor GeneralSettings name it something the describes what you are storing
ie. NameKey, EmailKey;  "name_key", "email_key"
    private const string SettingsKey = "settings_key";
    private static readonly string SettingsDefault = string.Empty;

    #endregion


//contructor to get and set the Settings to be stored. Name the constructor 
something the describes what you are storing. It will be called on the Page where you want to use it.

//ie NameSettings, EmailSettings     public static string GeneralSettings
    {
      get
      {
//SettingsKey is the string from the const unique name variable above
        return AppSettings.GetValueOrDefault(SettingsKey, SettingsDefault);
      }
      set
      {
        AppSettings.AddOrUpdateValue(SettingsKey, value);
      }
    }

  }
}

Now to use the Settings.cs -  you need include that namespace in the file where you want to use it. ie
//need to use namespace from where settings were SET to access it and use it to store user input.  roosspace is name of your project.
using $rootnamespace$.Helpers;


To save it: use the syntax below to save the data in Save button c file.
Use Settings is the file name where the code behind of get and sets.
Drain1LocationSettings is the GeneralSettings
EntryorPicker is in xaml file where the user enters the data needs to be save.
Settings.GeneralSettings = EntryorPicker;

To display the saved data: go to page or file where you want the saved data to be displayed then enter format syntax below
Labelofsavedata.Text = Settings.GeneralSettings;



https://www.youtube.com/watch?v=7DFCMm_4Nro


To clear IOS emulator settings:
delete app from emulator, clean all, then re run app.


http://bsubramanyamraju.blogspot.com/2018/04/appsettings-how-to-store-user-settings.html


No comments:

Post a Comment