2

I have a problem with WPF : I would like use a slider in my XAML with an default value but when i do it, an exception is thrown :

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in PresentationFramework.dll

this is my code:

<Slider Height="23" HorizontalAlignment="Center" Name="sliderVolume" VerticalAlignment="Top" Width="66" Grid.Row="2" ValueChanged="volume_ValueChanged" Margin="598,35,12,0" Value="100"/>

and this is my code is .cs :

private void volume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    mediaElement.Volume = sliderVolume.Value;
}

without the default value

Value"100"

It works !

Nathan Hillyer
  • 1,959
  • 1
  • 18
  • 22
Gims
  • 65
  • 2
  • 7
  • Maybe you should post the `volume_ValueChanged` event code. – Abdusalam Ben Haj Feb 07 '13 at 14:57
  • private void volume_ValueChanged(object sender, RoutedPropertyChangedEventArgs e) {mediaElement.Volume = sliderVolume.Value;} – Gims Feb 07 '13 at 15:02
  • Have you tried enabling "first chance" exceptions in the debugger, to catch the precise moment when the exception occurs? http://stackoverflow.com/questions/116896/visual-studio-how-to-break-on-handled-exceptions Try that and update the question with some additional details. – BTownTKD Feb 07 '13 at 15:06

3 Answers3

1

I don't see a minimum and maximum value defined. Your "default" value may be out of range.

BTownTKD
  • 7,911
  • 2
  • 31
  • 47
1

The MediaElement volume property is represented on a linear scale between 0 and 1.

Try:

mediaElement.Volume = sliderVolume.Value / 10;
Nathan Hillyer
  • 1,959
  • 1
  • 18
  • 22
1

According to MSDN . Maximum volume allowed is 1.

The media's volume represented on a linear scale between 0 and 1. The default is 0.5.

So you need to set your slider like this :

<Slider Value="0.5" Minimum="0" Maximum="1" Height="23" HorizontalAlignment="Center"
Name="sliderVolume" VerticalAlignment="Top" Width="66" Grid.Row="2"
ValueChanged="volume_ValueChanged" Margin="598,35,12,0" />

Code :

private void volume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> args)
      {
         mediaElement.Volume = (double)sliderVolume.Value;
      }
Abdusalam Ben Haj
  • 5,343
  • 5
  • 31
  • 45
  • Always the same, it works fine, nevertheless an exception is thrown without blocks try{} catch{} – Gims Feb 07 '13 at 15:32
  • ok it's works fine now. In my xaml I declared my mediaElement after my slider, so when I ran my projet, mediaElement was NULL. I'm sorry and Thank you very much. – Gims Feb 07 '13 at 15:44