如何添加MediaElement的进度条

5

我有一个电视剧项目,正在使用MediaElement。但是我需要帮助来添加寻找栏。如何添加?如何使寻找栏滑块像MediaPlayerLauncher?

我尝试了一些方法,比如:

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <MediaElement x:Name="player1" MediaOpened="player1_MediaOpened" CurrentStateChanged="player1_CurrentStateChanged" />
        <ListBox x:Name="alternatifliste" FontSize="20" FontStyle="Italic" FontWeight="Bold" Foreground="White" Margin="677,98,10,153" SelectionChanged="alternatifliste_SelectionChanged" />

    </Grid>

    <Button x:Name="play" Content="play" HorizontalAlignment="Left" Margin="94,336,0,0" Grid.Row="1" VerticalAlignment="Top" Click="play_Click"/>
    <Button x:Name="stop" Content="stop" HorizontalAlignment="Left" Margin="331,336,0,0" Grid.Row="1" VerticalAlignment="Top" Click="stop_Click"/>
    <Slider Name="timelineSlider"  Margin="0,68,0,244" Grid.Row="1" ValueChanged="timelineSlider_ValueChanged" />
</Grid>

我的项目: 我的项目 它的代码:
private void player1_MediaOpened(object sender, RoutedEventArgs e)
{
    TimeSpan ts = player1.NaturalDuration.TimeSpan;
    timelineSlider.Maximum = ts.TotalSeconds;
    timelineSlider.SmallChange = 1;
    timelineSlider.LargeChange = Math.Min(10, ts.Seconds / 10);
}

private void seekBar_DragCompleted(object sender, DragCompletedEventArgs e)
{
    player1.Position = TimeSpan.FromSeconds(timelineSlider.Value);
}

private void timelineSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    player1.Position = TimeSpan.FromSeconds(timelineSlider.Value);

}

private void player1_CurrentStateChanged(object sender, RoutedEventArgs e)
{
    //timelineSlider.Value = player1.Position.Seconds;
}

我希望像这样: 需要的按钮
1个回答

4

如果您希望它看起来像那个幻灯片,只需编辑样式即可。我在需要更改的位置添加了注释。

App.xaml 中,应该是这样的。

<Application>
    <!--Application Resources-->
    <Application.Resources>
        <Style x:Key="Chubs_SliderStyle" TargetType="Slider">
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="Maximum" Value="10"/>
            <Setter Property="Minimum" Value="0"/>
            <Setter Property="Value" Value="0"/>
            <Setter Property="Background" Value="{StaticResource PhoneChromeBrush}"/>
            <Setter Property="Foreground" Value="{StaticResource PhoneAccentBrush}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Slider">
                        <Grid Background="Transparent">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver"/>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0" To="0.1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="HorizontalTrack"/>
                                            <DoubleAnimation Duration="0" To="0.1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="VerticalTrack"/>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="HorizontalFill">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="VerticalFill">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Grid x:Name="HorizontalTemplate" Margin="{StaticResource PhoneHorizontalMargin}">

                                <!-- **** Change the height of these two rectangles to make the slider smaller -->
                                <Rectangle x:Name="HorizontalTrack" Fill="{TemplateBinding Background}" Height="12" IsHitTestVisible="False" Margin="0,22,0,50"/>
                                <Rectangle x:Name="HorizontalFill" Fill="{TemplateBinding Foreground}" Height="12" IsHitTestVisible="False" Margin="0,22,0,50">
                                    <Rectangle.Clip>
                                        <RectangleGeometry Rect="0, 0, 6, 12"/>
                                    </Rectangle.Clip>
                                </Rectangle>

                                <!-- **** this is the white rectangle thing, change the height and width if you want to make it smaller -->
                                <Rectangle x:Name="HorizontalCenterElement" Fill="{StaticResource PhoneForegroundBrush}" HorizontalAlignment="Left" Height="24" Margin="0,16,0,44" Width="12">
                                    <Rectangle.RenderTransform>
                                        <TranslateTransform/>
                                    </Rectangle.RenderTransform>
                                </Rectangle>


                            </Grid>
                            <Grid x:Name="VerticalTemplate" Margin="{StaticResource PhoneVerticalMargin}">
                                <Rectangle x:Name="VerticalTrack" Fill="{TemplateBinding Background}" IsHitTestVisible="False" Margin="18,0,18,0" Width="12"/>
                                <Rectangle x:Name="VerticalFill" Fill="{TemplateBinding Foreground}" IsHitTestVisible="False" Margin="18,0,18,0" Width="12">
                                    <Rectangle.Clip>
                                        <RectangleGeometry Rect="0, 0, 12, 6"/>
                                    </Rectangle.Clip>
                                </Rectangle>
                                <Rectangle x:Name="VerticalCenterElement" Fill="{StaticResource PhoneForegroundBrush}" Height="12" Margin="12,0,12,0" VerticalAlignment="Top" Width="24">
                                    <Rectangle.RenderTransform>
                                        <TranslateTransform/>
                                    </Rectangle.RenderTransform>
                                </Rectangle>
                            </Grid>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>
</Application>

现在,您可以在任何xaml文件中引用您的新样式。
<Slider Style="{StaticResource Chubs_SliderStyle}"></Slider>

你好@Chubosaurus Software,首先感谢您的回答。我正在尝试使用您的样式,但是出现了一些错误,如下所示: http://www.hizliresimyukle.com/images/2014/09/19/1a4ac5.jpg problem “指定的值无法分配给集合。期望以下类型:“UIElement”。 “Key”属性只能用于包含在“IDictionary”中的元素上。 不能将“Style”类型的值添加到“UIElementCollection”类型的集合或字典中。” 我该怎么解决它? - Murat KA
哦呵呵,你不能像那样添加样式。将代码剪切并粘贴到解决方案文件夹中的App.xaml文件中。我会更新我的答案。 - Chubosaurus Software
@DesperadoMurat np :), 如果这个方法对您有用,能否将其标记为解决方案?很高兴见到另一个WP开发者。 - Chubosaurus Software
哦,我忘记了,抱歉 :) - Murat KA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接