Skip to main content
Home Forums Silverlight Programming Programming with .NET - General weird exception adding an item from a TimelineMarkerCollection to MediaElement.Markers
9 replies. Latest Post by BigDubb on December 9, 2008.
(0)
silverbyte
Participant
1338 points
405 Posts
09-15-2008 4:13 AM |
Hello,
I want to add a collection of TimelineMarker objects to MediaElement.Markes collection on the MediaOpened event:
void mediaElement_MediaOpened(object sender, RoutedEventArgs e) { this.mediaElement.Markers.Clear(); foreach(TimelineMarker marker in this.markers) { this.mediaElement.Markers.Add(marker); // generates exception }
}
where markers is a TimelineMarkerCollection object which is created before the event is generated (in fact before setting MediaElement.Source).
This however results in an exception generated by Add method:
System.ArgumentException: Value does not fall within the expected range. at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at System.Windows.PresentationFrameworkCollection`1.AddDependencyObject(DependencyObject value) at System.Windows.Media.TimelineMarkerCollection.Add(TimelineMarker value) at TestProject.MediaPlayer.mediaElement_MediaOpened(Object sender, RoutedEventArgs e)
But if I create a new TimelineMarker object everything is fine:
void mediaElement_MediaOpened(object sender, RoutedEventArgs e) { this.mediaElement.Markers.Clear(); foreach(TimelineMarker marker in this.markers) { TimelineMarker tlm = new TimelineMarker(); tlm.Text = marker.Text; tlm.Time = marker.Time; tlm.Type = marker.Type; this.mediaElement.Markers.Add(tlm); }
I don't understand why a copy of the TimelineMarker created locally works but not an item from a TimelineMarkerCollection.
robhouwe...
Contributor
3158 points
540 Posts
09-15-2008 8:46 AM |
Is the markers collection already added to a mediaelement?
An element can only have 1 parent, that's probably why the error occurs.
(If this has answered your question, please click on mark as answer on this post)Cheers!Rob Houweling
09-15-2008 11:14 AM |
Hi,
thanks for your comment.
markers collection is a member type of my movie player class:
TimelineMarkerCollection markers = new TimelineMarkerCollection();
The clients of my class can add new markers and I thought to save them in a TimelineMarkerCollection. Then, I receive the event that movie is opened so I can add each TimelineMarker in this markers collection to the MediaElement.Markers
09-15-2008 1:28 PM |
Can you post your project?
09-16-2008 4:21 AM |
Here it is: https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=nitescua&ReleaseId=1545
I only have a MediaElement for which I add a marker in MediaOpened event.
As you can see it fails on adding a TimelineMarker object from a TimelineMarkerCollection.
09-16-2008 6:41 PM |
anybody knows why it crashes when using an object from TimlineMarkerCollection
Yi-Lun L...
All-Star
25052 points
2,747 Posts
09-17-2008 2:33 AM |
Hello, the problem is: When you add the TimelineMarker to a TimelineMarkerCollection, the TimelineMarkerCollection will become the logical parent of the TimelineMarker. A FrameworkElement can only have a single parent, so later when you add the TimelineMarker to the MediaElement, an exception is thrown. You can use a List<TimelineMarker> instead of a TimelineMarkerCollection. A List<TimelineMarker> will not be considered in the logical tree, and thus won't have this problem.
09-17-2008 3:41 AM |
Ok, thanks for explanation.
But it's a pitty that you need to add one by one a TimelineMarker to MediaElement.Markers instead of adding a collection
BigDubb
Member
65 points
77 Posts
12-05-2008 4:27 PM |
I too am getting an odd error.
This is the message information that is being thrown, with no inner exception at all.
System.ExecutionEngineException was unhandled Message="Exception of type 'System.ExecutionEngineException' was thrown." InnerException:
The code that generates the error.
public void AddMarker(TimeSpan Time) { //If the media already has a marker at that time, throw an error var _x = (from _m in _media.Markers where _m.Time == Time select _m); if(_x.Count() > 0) throw new MediaMarkerException("Marker already exists for this time"); else { TimelineMarker _tl = new TimelineMarker(); _tl.Time = Time; _media.Markers.Add(_tl); }
I just want to allow for users to create a new marker item for a piece of media.
12-09-2008 10:29 AM |
I found the problem, and thought I'd share.
When adding TimelineMarkers to a piece of media the media must be loaded, not just the source defined. What I ended up doing was creating a keypair list on the control that housed the media item, and on the MediaLoaded event I go through the list and add the markers.