locked
Programatically retrieving the values from Settings in Windows 8.1

    Question

  • Hi,

    In Windows SDK Samples (XAML Application Settings) I am interested in the functionality of the "Add a Settings Flyout".

    I would like to add various controls such as text box, combo box, toggle control, button, radio button to a fly out page in Setings in my application.

    Now I want to know where are these settings saved and how do I retrieve them programatically. For e.g. If i set a drop down box to choose the theme and the user selects it when the application is running, I should be able to retrive the value that the user selected and apply that value to the application. same for anyother control. Think of it as a Settings pane for the application's appearance and behaviour.

    My requirement is to be able to let the user enter the URL for accessing REST service (testing or production). How do I provide this functionality via the Settings dialog in Windows 8.1.

    Thanks,

    Rajesh.
    Wednesday, September 3, 2014 12:55 PM

Answers

All replies

  • Where to save the data will depend on the app but local or roaming app settings are typical. I'd chose roaming first for most settings.

    See

    Quickstart: Local application data (C#/VB/C++)

    Quickstart: Roaming application data (C#/VB/C++)

    Application data overview

    Quickstart: Add app settings (XAML) (demonstrates adding controls, but not saving the data)

     
    Wednesday, September 3, 2014 2:31 PM
    Owner
  • Hi Rajesh,

    For that you can use Windows.Storage API to Save Settings and Retrieve.

    In the Settings Code behind. This will save the settings to storage.

    using Windows.Storage

    ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings; 
                if (myComboBox.SelectedIndex == 0)
                {
                    localSettings.Values["Background"] = "Blue";
                }
                else if (myComboBox.SelectedIndex == 1)
                {
                    localSettings.Values["Background"] = "Yellow";
                }
                else if (myComboBox.SelectedIndex == 2)
                {
                    localSettings.Values["Background"] = "Black";
                }
                else
                {
                    localSettings.Values["Background"] = "Red";
                }

    and In MainPage code behind. For retrieving changes in Settings, if contains a certain background. Then change Background to that color.

    string Background = (string)localSettings.Values["Background"];
                if (Background != null && Background.Equals("Blue"))
                {
                    //Make page Background = blue;
                }
                else if (Background != null && Background.Equals("Yellow"))
                {
                    //Make page Background = yellow;
                }
                else if (Background != null && Background.Equals("Black"))
                {
                    //Make page Background = black;
                }
                else if (Background != null && Background.Equals("Red"))
                {
                    //Make page Background = red;
                }
                else
                {
                    //Make page Background = white;
                }

    Customize the above code to your own app style.

    More details: http://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.storage.applicationdata.localsettings.aspx

    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh700361.aspx

    Thank you.

    Wednesday, September 3, 2014 2:32 PM
  • Hi,

    Thanks a lot for this reply and I was able to achieve my goal. However I saw a certain behaviour.

    Most applications have a menu setting for preferences/Settings that can be set/modified from the home page. Once the user logins to the application, he/she can no longer access that settings page since modifying those values will affect the functioning of the application. This menu setting has been set in such a way that it is accessible only from the home page and not anywhere else.

    However in Windows 8.1 once the custom settings flyout is initiated in the home page it seems to be accessible from any where in the application and this is not the intended way. Is there any way for this custom settings fly out to be accessible ONLY from the home page and disabled/locked once the user logins to the application.

    Thanks,

    Rajesh.
    Thursday, September 4, 2014 1:42 PM
  • Please post separate questions in separate threads.

    Typically the settings pane is used for app global settings and is available everywhere. See http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh770544.aspx

    That said, the app gets called to add its commands and can chose to add some commands on the main page and others (or none) elsewhere. 

    Thursday, September 4, 2014 3:34 PM
    Owner