I created boolean variable for sound with app settings in this msdn articles. Now I want it in my menu as toggleswitch which can set volume on or off. I am thinking about using something like this this answer. But I am not sure if it is good for my problem. Should I add a variable for image to my AppSettings or is there a better way for doing this?
My solution:
In xaml:
<ToggleButton Name="tgbSound" BorderThickness="0" Background="Transparent"
Checked="tgbSound_Checked"
Unchecked="tgbSound_Unchecked"
IsChecked="{Binding Source={StaticResource appSettings}, Path=SoundSetting, Mode=TwoWay}">
</ToggleButton>
In code for xaml page:
private void tgbSound_Checked(object sender, RoutedEventArgs e)
{
SetTgbSoundContentTo("Images/volumeon.png");
}
private void tgbSound_Unchecked(object sender, RoutedEventArgs e)
{
SetTgbSoundContentTo("Images/volumeoff.png");
}
private void SetTgbSoundContentTo(string uri)
{
Image volumeoff = new Image();
ImageSource zdroj = new BitmapImage(new Uri(uri, UriKind.Relative));
volumeoff.Source = zdroj;
volumeoff.Height = 40;
if (tgbSound == null)
return;
tgbSound.Content = volumeoff;
tgbSound.Background = new SolidColorBrush(Colors.Transparent);
}
You can do both. For both the methods, I will suggest you a common approach.
Create a Boolean property in your AppSettings and create its wrapper inside your viewmodel.
Then bind (two way) the wrapper property in your view model to your UI element, that can be both a toggle switch or an image. In case of
toggleswitch, thetwo way bindingwill itself do the trick, but for an image you will have to handle the tap event and handle the on/off states and Boolean values in the code yourself.