0

I am working in C# and Silverlight 5, and I am trying to display one or more audio and/or video files that have been retrieved from a database table as a byte array. I decided the best way to get those byte arrays into a usable format was to create an ObservableCollection list in my ViewModel that gets populated during the service call:

mediaFiles = new ObservableCollection<MediaElement>();
foreach (FileUpload fu in FileUploadMediaTable)
{
   using (MemoryStream ms = new MemoryStream(fu.bytes, 0, fu.bytes.Length))
   {
      MediaElement me = new MediaElement();
      me.SetSource(ms);
      mediaFiles.Add(me);
   }
}

Now, my only problem is figuring out how to add these media elements into the view. I was thinking of creating a new ItemsControl with a custom DataTemplate that defines the buttons for playback that embeds the MediaElements in a StackPanel, but how would I associate each of the buttons with that specific MediaElement?

EDIT: Of course, I guess I could just create a list of byte arrays and add a MediaElement object to the DataTemplate and pass the byte array in the source with a Bytes2ImageConverter defined. Of course, maybe I could skip that step and just bind the MediaElement's source to a MemoryStream object created on each byte array. I am not sure of the best way to proceed (or if this stuff is even possible).

4

1 回答 1

1

您可以创建一个模型类,该类将包含媒体源(将绑定到 MediaElement Source 属性)和所需的附加数据(标题、作者等)。您可以使用列表框来显示所有这些视频。您应该创建一个项目模板并将其用于列表框项目。这个项目模板将包含一个媒体元素和一个播放/停止按钮,您将在其中绑定一个命令。命令将放置在您的 ViewModel 类中,您可以将模型绑定为命令参数。因此,您可以访问应该播放的媒体源(模型类)。播放/停止视频怎么样:您可以创建一个媒体元素助手,它将包含一个附加的依赖属性(例如:IsPlaying,在您的模型类中)。您将为播放/停止视频绑定一个真/假值。就这样。

于 2012-07-04T05:31:44.060 回答