EnvelopedDevil
EnvelopedDevil

Reputation: 658

MediaElement Source in codebehind

Why does this code produce error

wpf code

<MediaElement x:Name="Player" LoadedBehavior="Manual"/>

c# code

Player.Source = new Uri(@"C:\Users\Georgi\AppData\Local\VideoPresenter\Content\1.mp4", UriKind.RelativeOrAbsolute);

The error produced is

An exception of type 'System.NullReferenceException' occurred in VideoPresenter.exe but was not handled in user code

Additional information: Object reference not set to an instance of an object.

Upvotes: 0

Views: 3135

Answers (1)

EnvelopedDevil
EnvelopedDevil

Reputation: 658

The problem was that The InitializeCompoent() was at the end of the constructor.

Player.Source = new Uri(tempPath, UriKind.RelativeOrAbsolute);
Player.Play();
InitializeComponent();

And the InitializeComponent should be at the very top like this.

InitializeComponent();
Player.Source = new Uri(tempPath, UriKind.RelativeOrAbsolute);
Player.Play();

Upvotes: 2

Related Questions