存储文件到系统uri?

时间:2013-05-13 20:00:17

标签: c# windows-8 windows-runtime storagefile

我有一个用c#编写的Windows Metro应用程序。

以下是我用来从本地音乐库中选择文件的代码:

FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.MusicLibrary;
openPicker.FileTypeFilter.Add(".mp3");
openPicker.FileTypeFilter.Add(".wav");

StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
   myMediaElement.Source = file; //error here
}
else
{
    //error here
}

它表示StorageFile无法转换为用于更改MediaElement源的System.Uri。如何让我的文件成为uri链接? Uri("...")似乎只接受文件位置所在的String

1 个答案:

答案 0 :(得分:8)

您必须使用文件流和SetSource。来自How to play a local media file using a MediaElement

var file = await openPicker.PickSingleFileAsync();
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
myMediaElement.SetSource(stream, file.ContentType);