WPF关于MediaElement的一些问题
下面是xaml代码能运行! 我想请教一下坛子里面的高手下面这个xaml用后台代码来写怎么实现?就是把下面的xaml实现的功能全部写成C#代码,怎么写?请大家给个事例。
我刚开始学习WPF
<Window x:Class="MediaPlay.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" SizeToContent="WidthAndHeight"> <Window.Triggers> <EventTrigger RoutedEvent="Button.Click" SourceName="BtnPlay"> <EventTrigger.Actions> <BeginStoryboard x:Name="mbPlayer1"> <Storyboard> <MediaTimeline Source="../../1002.mp3" Storyboard.TargetName="mediaElement1"/> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> <EventTrigger RoutedEvent="Button.Click" SourceName="BtnPause"> <EventTrigger.Actions> <PauseStoryboard BeginStoryboardName="mbPlayer1"/> <BeginStoryboard x:Name="mbPlayer2"> <Storyboard> <MediaTimeline Source="../../1003.mp3" Storyboard.TargetName="mediaElement2"/> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> <EventTrigger RoutedEvent="Button.Click" SourceName="BtnResume"> <EventTrigger.Actions> <ResumeStoryboard BeginStoryboardName="mbPlayer1"/> <StopStoryboard BeginStoryboardName="mbPlayer2"/> </EventTrigger.Actions> </EventTrigger> </Window.Triggers> <StackPanel HorizontalAlignment="Center" Margin="20"> <Border BorderThickness="3" Background="Black"> <Border.BorderBrush> <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"> <GradientStop Offset="0" Color="White"/> <GradientStop Offset="0.5" Color="Gold"/> </LinearGradientBrush> </Border.BorderBrush> <MediaElement Width="450" x:Name="mediaElement1" LoadedBehavior="Manual" Volume="{Binding ElementName=volumeSlider,Path=Value}"> </MediaElement> </Border> <Border> <Border.BorderBrush> <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"> <GradientStop Offset="0" Color="White"/> <GradientStop Offset="0.5" Color="Gold"/> </LinearGradientBrush> </Border.BorderBrush> <MediaElement Width="450" x:Name="mediaElement2" LoadedBehavior="Manual" Volume="{Binding ElementName=volumeSlider,Path=Value}"> </MediaElement> </Border> <StackPanel Orientation="Horizontal" Height="40" HorizontalAlignment="Center"> <Button x:Name="BtnPlay" Content="Play" Style="{DynamicResource btnStyle}"/> <Button x:Name="BtnPause" Content="Pause" Style="{DynamicResource btnStyle}"/> <Button x:Name="BtnResume" Content="Resume" Style="{DynamicResource btnStyle}"/> </StackPanel> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="5"> <TextBlock Text="Volume" Foreground="Gold"/> <Slider x:Name="volumeSlider" Minimum="0" Maximum="1" Value="0.5" Width="200"/> </StackPanel> </StackPanel></Window>
private void BtnPlay_Click(object sender, RoutedEventArgs e)//BtnPlay的点击事件 { mediaElement1.Source = new Uri(AppDomain.CurrentDomain.BaseDirectory + "media/1002.mp3"); mediaElement1.Play(); } private void BtnPause_Click(object sender, RoutedEventArgs e)//BtnPause的点击事件 { mediaElement1.Pause(); mediaElement2.Source = new Uri(AppDomain.CurrentDomain.BaseDirectory + "media/1003.mp3"); mediaElement2.Play(); } private void BtnResume_Click(object sender, RoutedEventArgs e)//BtnResume的点击事件 { mediaElement1.Play(); mediaElement2.Stop(); }