c# WPF文件信息媒体源
本文关键字:媒体 信息 文件 WPF | 更新日期: 2023-09-27 18:12:35
当我得到。mp3文件没有路径,但我不能播放Uri得到一些错误。我的代码;
private void listbox2_Drop(object sender, DragEventArgs e)
{
try
{
string[] a = (string[])(e.Data.GetData(DataFormats.FileDrop, false));
foreach (var names in a)
{
FileInfo fileInfo = new FileInfo(names);
if (fileInfo.Extension == ".MP3" ||fileInfo.Extension == ".mp3")
{
listbox2.Items.Add( fileInfo.Name);
}
}
}
catch (Exception)
{}
}
private void listbox2_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
object listboxİtems = listbox2.SelectedItem;
if (listboxİtems != null)
{
media2.Source = new Uri(listboxİtems.ToString(), UriKind.Relative); // I'm getting erors here
media2.Play();
}
}
他在media2处得到错误。listboxItems.ToString()字符串无法播放
只需将完整的FileInfo
对象添加到Items中。
然后你有完整的对象来构建你的Uri
与FullPath
或DirectoryName
和Name
。
最后,将ListBox.ItemTemplate
定义为适合您需要的内容(例如将TextBlock
绑定到Name
属性)
private void listbox2_Drop(object sender, DragEventArgs e)
{
try
{
string[] a = (string[])(e.Data.GetData(DataFormats.FileDrop, false));
foreach (var names in a)
{
FileInfo fileInfo = new FileInfo(names);
if (fileInfo.Extension == ".MP3" ||fileInfo.Extension == ".mp3")
{
listbox2.Items.Add(fileInfo); //store the full FileInfo in your Items, not just the Name property
}
}
}
catch (Exception)
{}
}
private void listbox2_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
FileInfo listboxItem = listbox2.SelectedItem as FileInfo; //cast the SelectedItem to the FileInfo type
if (listboxItem != null)
{
media2.Open(new Uri(listboxItem.FullPath, UriKind.RelativeOrAbsolute)); // if I remember well, Source is Read-Only, use Open instead
media2.Play();
}
}
和建议的ListBox的XAML:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>