如何在Silverlight中动态设置MediaElement位置?

时间:2010-03-25 15:23:42

标签: c# silverlight silverlight-3.0 mediaelement

我正在尝试通过以下代码设置媒体元素位置:

MediaElement musicPlayer = new MediaElement();
musicPlayer.Position =  new TimeSpan(0, 0, 30);                        
musicPlayer.Source = new Uri(strMediaFileURL, UriKind.RelativeOrAbsolute);
LayoutRoot.Children.Add(musicPlayer);

令人惊讶的是,在调试时,musicPlayer.Position保持不变,有什么帮助吗?

谢谢, Subhendu

1 个答案:

答案 0 :(得分:2)

试试这样: -

MediaElement musicPlayer = new MediaElement();
musicPlayer.MediaOpened += (s, args) =>
{
    var player = (MediaElement)s;
    if (player.CanSeek)
        player.Position =  new TimeSpan(0, 0, 30);   
}                     
musicPlayer.Source = new Uri(strMediaFileURL, UriKind.RelativeOrAbsolute);
LayoutRoot.Children.Add(musicPlayer);