Windows Phone 8.1 Saving ToggleSwitch Status

0

I have a code where I need to save the state of ToggleSwitch. In order to start the page, the Ison property is active or inactive, depending on the user's selection.

To save the state of the element I have used '' Windows.Storage.ApplicationData.Current.LocalSettings; '' which is the replacement for IsolatedStorageSettings for WP8.1.

Follow the Cod. Below:

  using Windows.Storage;

  namespace TESTE
 {

public sealed partial class opcoes : Page
{
   public ApplicationDataContainer settings = Windows.Storage.ApplicationData.Current.LocalSettings;

    public opcoes()
    {
        statusToggles();
        this.InitializeComponent();          
        this.navigationHelper = new NavigationHelper(this);
        this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
        this.navigationHelper.SaveState += this.NavigationHelper_SaveState;

     }
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        this.navigationHelper.OnNavigatedTo(e);
        statusToggles();
    }       

    // Evento disparado pelo ToggledSwitch
    private void bgaleatorio_Toggled(object sender, RoutedEventArgs e)
    {
        ToggleSwitch toggleSwitch = sender as ToggleSwitch;
        bgpar(toggleSwitch);

    }
    // statusToggles() -> Verifica se  bgaleatorio for 1 ou 0 o Toggle será ativo ou inativo respectivamente
    private void statusToggles()
    {
        Object tempsetings = settings.Values["bgaleatorio"];
        if (tempsetings == "1") { bgaleatorio.IsOn = true; }
        if (tempsetings == "0") { bgaleatorio.IsOn = false; }

    }
    // bgpar -> Recebe um ToggleSwitch como parametro e Grava em settings.Values["bgaleatorio"] os valores 0 ou 1  de acordo com o Statatus do ToggleSwitch
    private void bgpar(ToggleSwitch toggleSwitch)
    {           
        if (toggleSwitch.IsOn == true)
        {
            settings.Values["bgaleatorio"] = "1";
        }

        else if (toggleSwitch.IsOn == false)
        {
            settings.Values["bgaleatorio"] = "0";
        }

    }       

}     
}

Xaml Code:

<ToggleSwitch x:Name="bgaleatorio" Header="Background Aleatório" HorizontalAlignment="Left" Margin="68,123,0,0" VerticalAlignment="Top" Width="287" IsOn="True" FontSize="11" Grid.RowSpan="2" Toggled="bgaleatorio_Toggled" />
    
asked by anonymous 25.11.2014 / 01:39

2 answers

1

I did it!

I created the Toggle in code behind, had to stall it before trying to assign some property.

#

    

27.11.2014 / 16:57
-1

It would be best to pass statusToggles(); to Loaded of the page.

    
19.02.2016 / 15:33