I have a small app that I am using to try and tame the Volume property of the MediaElement.
The real app that I need to fix is a media player app but it has so much code/XAML I am using the "test" app to ensure that things will work before putting the code into it.
First the XAML:
<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:BindingToAttachedProperty" Title="MainWindow" Height="350" Width="525"><Grid local:AttachedProperties.AllVolume=".5" x:Name="theGrid"><Grid.RowDefinitions><RowDefinition Height="Auto"></RowDefinition><RowDefinition></RowDefinition></Grid.RowDefinitions><StackPanel Orientation="Horizontal" Grid.Row="0" ><Slider Value="{Binding ElementName=theGrid , Path=(local:AttachedProperties.AllVolume),Mode=TwoWay }" Maximum="1" Width="200" LargeChange="0.1" TickFrequency="0.1"></Slider><Button x:Name="theButton" Click="theButton_Click" Margin="20,0,0,0">Go Up</Button><Button x:Name="theDButton" Click="theDButton_Click" Margin="20,0,0,0">Go Up</Button><TextBlock Text="{Binding ElementName=theGrid , Path=(local:AttachedProperties.AllVolume)}" Grid.Row="1" Margin="20,0,0,0"></TextBlock></StackPanel><MediaElement x:Name="theMedia" Grid.Row="1" Volume="{Binding ElementName=theGrid , Path=(local:AttachedProperties.AllVolume)}" Source="H:\Movies\Frozen.mp4"></MediaElement></Grid></Window>
Now in the sample app I have only one MediaElement (the real app has two and I am using a volume change on each to achieve a crossover effect. The sample app is using an AttachedProperty to bind to the volume. This works as expected although VS 2013 has a hard time with the binding expression. (one would have thought by this iteration of VS it would deal with all bindings). If I start the app and using the slider it does in fact modify the volume of the MediaElement.
As I said above I am using two MediaElements to achieve crossover so when that happens there are two animations running, one to raise the volume of the new mp3 (the sample uses a .mp4 file but that is not the problem) and one to decrease the already playing mp3.
Now in the sample app I have two buttons which I am using to simulate the ending of the media.
The Code Behind:
Imports System.ComponentModel Imports System.Windows.Media.Animation Class MainWindow Private Sub theButton_Click(sender As Object, e As RoutedEventArgs) Dim sb As New Storyboard Dim da As New DoubleAnimation With {.From = theMedia.Volume, .To = 1.0, .Duration = TimeSpan.FromSeconds(10)} Storyboard.SetTarget(da, theGrid) Storyboard.SetTargetProperty(da, New PropertyPath(AttachedProperties.AllVolumeProperty)) sb.Children.Add(da) sb.Begin(Me, True) End Sub Private Sub theDButton_Click(sender As Object, e As RoutedEventArgs) Dim sb As New Storyboard Dim da As New DoubleAnimation With {.From = theMedia.Volume, .To = 0.0, .Duration = TimeSpan.FromSeconds(10)} Storyboard.SetTarget(da, theGrid) Storyboard.SetTargetProperty(da, New PropertyPath(AttachedProperties.AllVolumeProperty)) sb.Children.Add(da) sb.Begin(Me, True) End Sub End Class
One button makes the volume go to 1.0 and the other makes it decrease to 0.0. Both buttons work.
Now the problem arises - once the animation (either one) happens the slider goes inactive. I have checked with Snoop and the slider is still enabled but you can no longer drag the thumb of the slider. The two animations of the volume still work so the binding is still there and the textblock and the slider values change with the animation.
Anyone have an idea why the slider seems to go inactive?
Lloyd Sheen