Windows Phone 8.1 Saving Toggleswitch Status

Asked

Viewed 106 times

0

I have a code where I need to save the state of Toggleswitch. So that when starting the page it is with the Ison property active or inactive, according to how the user had selected.

But when starting this always assigning '1' that represents being active.

To save the state of the element I have used ''Windows.Storage.Applicationdata.Current.Localsettings;'' which is the replacement of 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";
        }

    }       
    
}     
}

Codigo Xaml:

<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" />
  • 2

    Puts a Breakpoint in Toggles() statusIn the line after Object tempsettings... and inspects the value that came in the variable.

  • 1

    I did that, after running the app by default the Ison property comes true. I changed to false Settings.Values["bgaleatorio"] received 0. Exit the page, when returning it calls the statusToggles. The same holds the value 0 of ["bgaleatorio"] but bgaleatorio.Ison comes null and it does not apply true or false if ["bgaleatorio"] is 1 or 0 respectively.

  • 1

    Code Xaml: <Toggleswitch x:Name="bgaleatorio" Header="Random Background" Horizontalalignment="Left" Margin="68,123,0,0" Verticalalignment="Top" Width="287" Ison="True" Fontsize="11" Grid.Rowspan="2" Toggled="bgaleatorio_Toggled"/>

  • 1

    http://meta.pt.stackoverflow.com/questions/297/quando-se-deve-colocaro-nome-da-linguagem-no-t%C3%adtulo/1911#1911 And when adding information, prefer [Edit] the question. It is very difficult to read certain information in comments, besides fragmenting the information.

2 answers

1


-1

The best thing would be to pass the statusToggles(); to the Loaded page.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.