Home > Enterprise >  Change Theme with Switch Toggled
Change Theme with Switch Toggled

Time:09-30

Am trying to change the Theme between Light and Dark with a Switch.

With the Toggled event it goes to Dark , but when i switch it off not back to Light.

I think i have to use If ?

 <Switch x:Name="Darkswitch"
            Toggled="Darkswitch_Toggled"
            IsToggled="False"
            OnColor="White"
            ThumbColor="Gray"
            VerticalOptions="Center"
            HorizontalOptions="Center" />

and

 private void Darkswitch_Toggled(object sender, ToggledEventArgs e)
{    
        Application.Current.UserAppTheme = AppTheme.Dark;
}

CodePudding user response:

Check which is the current theme and then request the other upon toggle:

void Darkswitch_Toggled(object sender, ToggledEventArgs e)
{    
     AppTheme currentTheme = Application.Current.RequestedTheme;
     if (currentTheme == AppTheme.Dark)
        Application.Current.UserAppTheme = AppTheme.Light;
     else
        Application.Current.UserAppTheme = AppTheme.Dark;
}
  • Related