如何播放和暂停MediaElement?

时间:2019-07-08 08:55:08

标签: c# wpf video mediaelement

我想简短地播放MediaElement,然后在添加列表框项目时将其停止。 看起来像视频缩略图。我动态创建了一个列表框,可以播放并停止它吗?

Main.xaml

<ListBox Grid.Row="0" x:Name="listBox" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" AllowDrop="True" Drop="ListBox_Drop" DragEnter="ListBox_DragEnter" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Disabled" MouseMove="ListBox_MouseMove">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel IsItemsHost="True"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <MediaElement Margin="3" x:Name="{Binding vName}" Source="{Binding Path}" Height="64" Stretch="Uniform" IsMuted="True"/>
                    <TextBlock Margin="3" Text="{Binding Name}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

Main.xaml.cs

private void Add_Item(string file)
    {
        extension = Path.GetExtension(file);
        file_name = Path.GetFileName(file);
        if (extension.Equals(".mp4") || extension.Equals(".mwv") || extension.Equals(".avi"))
        {
            list.Add(new VideoListing()
            {
                Name = file_name,
                Path = file,
                vName = file_name,
            });
        }
        listBox.ItemsSource = list;
        listBox.Items.Refresh();
    }

VideoListing.cs

class VideoListing
{
    public string Name { get; set; }
    public string Path { get; set; }
    public string vName { get; set; }
}

0 个答案:

没有答案