Im trying to create an overlay system for media items. To do so I add a canvas to my form. Via code I add a media element to the canvas.
mediaPlayer = new MediaElement();
canvas.Children.Add(mediaPlayer);
mediaPlayer.HorizontalAlignment = HorizontalAlignment.Left;
mediaPlayer.VerticalAlignment = VerticalAlignment.Top;
mediaPlayer.Height = 1080;
mediaPlayer.Width = 1920;
mediaPlayer.Source = new Uri(ConfigurationManager.AppSettings["VideoFile"], UriKind.Relative);
mediaPlayer.LoadedBehavior = MediaState.Manual;
mediaPlayer.Play();
mediaPlayer.MouseDown +=mediaPlayer_MouseDown;
When I launch the app I get a Exception on the canvas.Children.Add(mediaPlayer): Object reference not set to an instance of an object. It does not mather if I place this line after the instantiate, right before the Play() call or just after the play() call.
I did some tests, when adding it in the designer it works, though you are not alow to start maunally, when setting then LoadedBehavior to manual it crashes also (same error). When adding the media element to the form it does work as expected, though now I cannot overlay elements... What am I doing wrong?