Mediaelement播放列表重复如何?

时间:2013-04-25 12:22:42

标签: c# wpf windows listbox repeat

我无法找到任何解决方案,以便在打开上一个文件时重置索引。 我有一个媒体元素从列表框中读取图像,当最后一个图像播放时,我希望它重新启动。像可重复的Mediaelement播放列表一样。 请帮助,我真的需要帮助。

    Dictionary<string, string> Listbox1Dict = new Dictionary<string, string>();
    public static List<string> images = new List<string> { ".JPG", ".JPE", ".BMP", ".GIF", ".PNG" }; // Bildtyper som stöds
    public static List<string> movies = new List<string> { ".WMV", ".WAV", ".SWF", ".MP4", ".MPG", ".AVI" }; // Filmtyper som stöds
    List<string> paths = new List<string>();
    DispatcherTimer dispatcherTimer = new DispatcherTimer();
    DispatcherTimer NextImageTimer = new DispatcherTimer();
    int x = 20; //Ställa in antal sekunder per bild
    private int currentSongIndex = -1;

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
            ShowNextImage();
    }
    private void dispatch()
    {
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = new TimeSpan(0, 0, 0, x);
    }

    private void ShowNextImage()
    {
        if (currentSongIndex == -1)
        {
            currentSongIndex = Listbox1.SelectedIndex;
        }
        currentSongIndex++;

        var selected = Listbox1.Items[currentSongIndex];
        string s = selected.ToString();
        if (Listbox1Dict.ContainsKey(s))
        {

            if (images.Contains(System.IO.Path.GetExtension(s).ToUpperInvariant()))
            {

                if (currentSongIndex < Listbox1.Items.Count)
                {
                    mediaElement1.Visibility = Visibility.Visible;
                    SearchBtn.Visibility = Visibility.Hidden;
                    Listbox1.Visibility = Visibility.Hidden;
                    FileNameTextBox.Visibility = Visibility.Hidden;
                    mediaElement1.Source = new Uri(Listbox1Dict[s]);
                    mediaElement1.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
                    mediaElement1.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
                    this.Background = new SolidColorBrush(Colors.Black);
                    this.WindowStyle = WindowStyle.None;
                    this.WindowState = WindowState.Maximized;
                    mediaElement1.StretchDirection = StretchDirection.Both;
                    mediaElement1.Stretch = Stretch.Fill;
                    dispatcherTimer.Start();

                }
            }
            else if (movies.Contains(System.IO.Path.GetExtension(s).ToUpperInvariant()))
            {
                if (currentSongIndex < Listbox1.Items.Count)
                {
                    dispatcherTimer.Stop();
                    mediaElement1.Visibility = Visibility.Visible;
                    Listbox1.Visibility = Visibility.Hidden;
                    FileNameTextBox.Visibility = Visibility.Hidden;
                    mediaElement1.Source = new Uri(Listbox1Dict[s]);
                    mediaElement1.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
                    mediaElement1.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
                    this.Background = new SolidColorBrush(Colors.Black);
                    this.WindowStyle = WindowStyle.None;
                    this.WindowState = WindowState.Maximized;
                    mediaElement1.StretchDirection = StretchDirection.Both;
                    mediaElement1.Stretch = Stretch.Fill;
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

你不能这样做:

private void ShowNextImage()
{
    if (currentSongIndex == -1)
    {
        currentSongIndex = Listbox1.SelectedIndex;
    }
    if (currentSongIndex == ListBox1.Items.Count)
    {
        currentSongIndex = 0;
    }
    currentSongIndex++;
    ....
}