推理:
我在这个网站上看到过几次重复这个问题,而且每个人似乎都会给人们带来相同的结论。
好的,我有一个位于窗口内的音乐播放器。当窗口打开时,将运行OpenFileDialog方法,用户必须选择.mp3文件。完成后,他们可以在导航程序中的其他窗口的同时播放文件。
我正在尝试:
在标签中输入所选文件的文件名。有了这个,我想显示正在播放的文件名。我只想显示文件名,而不是它的路径。
我在重复的问题上看到了什么:
重复修正,我可以使用以下代码来获取带或不带文件扩展名的文件名:
label1.Content = Path.GetFileName(media.Source.ToString());
label1.Content = Path.GetFileNameWithoutExtension(media.Source.ToString());
问题:
我试图在我的程序中输入这个,但我没有“路径”部分(我不太确定这部分代码的实际名称)。
我能得到的最接近的是:class System.Windows.Shapes.Path |绘制一系列相互连接的直线和曲线。
我的VS中似乎没有另一条“路径”,所以我不太清楚该怎么做。
代码(对不起这里的代码,复制到问题框有点尴尬)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Windows.Threading;
namespace WolfAlbumCatalogue
{
/// <summary>
/// Interaction logic for MusicPlayer.xaml
/// </summary>
public partial class MusicPlayer : Window
{
private MediaPlayer mediaPlayer = new MediaPlayer();
AlbumCatalogue albumCatalogue;
public MusicPlayer(AlbumCatalogue parent)
{
InitializeComponent();
albumCatalogue = parent;
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "MP3 files (*.mp3)|*.mp3|All files(*.*)|*.*";
if (openFileDialog.ShowDialog() == true)
mediaPlayer.Open(new Uri(openFileDialog.FileName));
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer_Tick;
timer.Tick += miniTimer_Tick;
timer.Start();
albumCatalogue.img_musicPlayerWindow.IsEnabled = false;
lbl_songName.Content = Path
}
private void rec_closeHover_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
this.Close();
albumCatalogue.img_musicPlayerWindow.IsEnabled = true;
}
void timer_Tick(object sender, EventArgs e)
{
if (mediaPlayer.Source != null)
{
lbl_timer.Content = String.Format("{0} / {1}", mediaPlayer.Position.ToString(@"mm\:ss"), mediaPlayer.NaturalDuration.TimeSpan.ToString(@"mm\:ss"));
}
else
{
lbl_timer.Content = "No file selected...";
}
}
void miniTimer_Tick(object sender, EventArgs e)
{
if (mediaPlayer.Source != null)
{
lbl_miniTimer.Content = String.Format("{0} / {1}", mediaPlayer.Position.ToString(@"mm\:ss"), mediaPlayer.NaturalDuration.TimeSpan.ToString(@"mm\:ss"));
}
else
{
lbl_miniTimer.Content = "No file selected...";
}
}
private void btn_play_Click(object sender, RoutedEventArgs e)
{
mediaPlayer.Play();
lbl_play_pause.Content = "Playing...";
lbl_play_pause.Visibility = Visibility.Visible;
}
private void btn_pause_Click(object sender, RoutedEventArgs e)
{
mediaPlayer.Pause();
lbl_play_pause.Content = "Paused.";
lbl_play_pause.Visibility = Visibility.Visible;
}
private void btn_stop_Click(object sender, RoutedEventArgs e)
{
mediaPlayer.Stop();
lbl_play_pause.Visibility = Visibility.Hidden;
}
private void rec_titleBar_MouseDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}
private void rec_windowBkg_MouseDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}
private void rec_btnOpenAudioFile_MouseDown(object sender, MouseButtonEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "MP3 files (*.mp3)|*.mp3|All files(*.*)|*.*";
if (openFileDialog.ShowDialog() == true)
mediaPlayer.Open(new Uri(openFileDialog.FileName));
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer_Tick;
timer.Start();
}
}
}
我也会复制 XAML ,但我使用混合动画,这个问题太长了。如果您需要,请告诉我:))
那就是说,我认为这就是一切!
谢谢
答案 0 :(得分:1)
在您的应用程序中包含标题System.IO。 (使用System.IO;)。如果找不到,则需要添加引用System dll。