Tenho um codigo onde preciso salvar o estado do ToggleSwitch. Para que ao iniciar a pagina o mesmo fique com a propriedade Ison ativa ou inativa, de acordo como o usuário havia selecionado.
Porém ao iniciar esta sempre atribuindo '1' que representa estar ativo.
Para salvar o estado do elemento tenho usado ''Windows.Storage.ApplicationData.Current.LocalSettings;'' que é o substituto do IsolatedStorageSettings para WP8.1.
Segue o Cod. Abaixo:
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" />