2013-04-23 16 views
2

我正在嘗試使用顯示列表框中xbox每個圖像的mediaElement進行幻燈片放映。WPF顯示每個圖像x秒如何?

如何讓我的代碼在繼續前x秒播放每個圖像?

此代碼將所有圖像到一個名爲ListBox1的

Dictionary<string, string> Listbox1Dict = new Dictionary<string, string>(); 

    private void SearchBtn_Click(object sender, RoutedEventArgs e) 
    { 
     Listbox1.Items.Clear(); 
     FolderBrowserDialog folderDialog = new FolderBrowserDialog(); 
     folderDialog.SelectedPath = "C:\\"; 

     DialogResult result = folderDialog.ShowDialog(); 
     if (result.ToString() == "OK") 
      FileNameTextBox.Text = folderDialog.SelectedPath; 
     string directory = FileNameTextBox.Text; 
     var files = Directory.GetFiles(directory).Where(name => !name.EndsWith(".ini")); 
     foreach (string file in files) 
     { 
      Listbox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file)); 
      Listbox1Dict.Add(System.IO.Path.GetFileNameWithoutExtension(file), file); 
     } 
    } 

列表框下面的代碼顯示在全屏模式下的所有圖像,但它在開始跳過大家最後的影像。

private void button1_Click_1(object sender, RoutedEventArgs e) 
{ 
    foreach (var selected in Listbox1.Items) 
     { 
      string s = selected.ToString(); 
      if (Listbox1Dict.ContainsKey(s)) 
      { 
       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; 
      } 

    } 
} 

試過這段代碼讓圖像逐個播放,但是出現錯誤。看在評論代碼:

private int currentSongIndex = -1; 

void mediaElement1next(object sender, EventArgs e) 
{ 
    if(currentSongIndex == -1) 
    { 
     currentSongIndex = Listbox1.SelectedIndex; 
    } 
    currentSongIndex++; 
    if(currentSongIndex < Listbox1.Items.Count) 
    { 
     mediaElement1.Play(Listbox1.Items[currentSongIndex]); // No overload for method 'Play' takes 1 arguments  
    } 
    else 
    { 
     // last song in listbox has been played 
    } 
} 
+0

你需要有引入停頓的地方。也許使用'AutoResetEvent'可能會有所幫助,也許可能以某種方式。 – code4life 2013-04-23 12:35:44

回答

0

我的想法是實現做當數到X,然後叫NEXTIMAGE()函數的線程。

+0

是的,這也是我一直在想的,但也必須有一個「下一個圖像控制」。 – Sneakybastardd 2013-04-23 11:53:32

1

將您的圖像路徑保存在列表中&使用計時器的tick事件。 是這樣的:

List<string> paths = new List<string>(); 

private void timer1_Tick(object sender, EventArgs e) 
{ 
    pictureBox1.Image = getNextImage(); 
} 

private string getNextImage() 
{ 
    //code... 
} 
enter code here 

編輯: 添加類變量:int索引= 0; 在SearchBtn_Click事件上,將結果添加到列表中。

//.. 
    foreach (string file in files) 
    { 
     paths.Add(file); 
    } 
//.. 

然後做正如我上面做了和getNextImage方法的內容將是:

private string getNextImage() 
{ 
    if(index < paths.Count - 1) 
    { 
     index += 1; 
    } 
    else 
    { 
     index = 0; 
    } 
    return paths[index]; 
} 
+0

hm無法使代碼正常工作...您能否通過我提供的代碼編寫示例? – Sneakybastardd 2013-04-23 12:02:15

+0

是的,我編輯了我的答案! – Yami 2013-04-23 12:20:09

+0

對不起,延遲迴復,但我使用WPF,並沒有任何圖片框存在那裏。 我正在使用mediaelement。 對此有任何想法? – Sneakybastardd 2013-04-24 22:23:34

1

我認爲你需要一個計時器設置你的下一個圖像。使用您當前使用的代碼,它將遍歷列表並更改圖像,直到完成。

看看DispatcherTimer。你可以設置它,在每次打勾時,它會變成下一張圖片。像這樣的東西(只寫了我的頭)

dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); 
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); 

然後,你的事件處理程序中:

private void dispatcherTimer_Tick(object sender, EventArgs e) 
{ 
    // get the next image 
} 

當然你也可以使用其他類型的定時器,但是這是主要的想法。

+0

是的,但它也必須找到下一個圖像,而不是直接跳到列表框的末尾...... – Sneakybastardd 2013-04-23 11:59:50

+0

而不是foreach,有一個索引'n',並在每次定時器滴答時增加它。然後,爲了得到你的圖像,你得到了列表框中的第n個元素,而不是迭代它。 – 2013-04-23 12:01:00

+0

更新我的代碼,再次檢查我的帖子。那麼我該如何處理這個問題呢? :/ – Sneakybastardd 2013-04-23 12:10:05

0

事情是這樣的:

 private void Button_Click_1(object sender, RoutedEventArgs e) 
     { 
      if (Listbox1.Items.Count > 0) 
      { 
       if (dispatcherTimer.IsEnabled) 
        dispatcherTimer.Stop(); 
       else 
       { 
        curImage = 0; 
        dispatcherTimer.Start(); 
       } 
      } 
     } 




private void dispatcherTimer_Tick(object sender, EventArgs e) 
{ 
    Dispatcher.Invoke((Action)delegate 
    { 
     ShowNextImage(); 
    }, null);    
} 



private void ShowNextImage() 
{ 
    if (curImage >= Listbox1.Items.Count) 
     curImage = 0; 

    var selected = Listbox1.Items[curImage]; 
    string s = selected.ToString(); 
    if (Listbox1Dict.ContainsKey(s)) 
    { 
     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; 
    } 
} 

和申報

DispatcherTimer dispatcherTimer = new DispatcherTimer(); 
     int x = 2; //seconds 
     private int curImage = 0; 

和一些結構

dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); 
      dispatcherTimer.Interval = new TimeSpan(0, 0, x); 
+0

對不起,延遲迴復,但代碼沒有處理下一張圖片.. – Sneakybastardd 2013-04-24 22:22:49