C# 存储文件到系统uri?

C# 存储文件到系统uri?,c#,windows-8,windows-runtime,storagefile,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

我有一个用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

您必须使用文件流以及
SetSource
。发件人:

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