Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# codebehind中的MediaElement源_C#_Wpf_Mediaelement - Fatal编程技术网

C# codebehind中的MediaElement源

C# codebehind中的MediaElement源,c#,wpf,mediaelement,C#,Wpf,Mediaelement,为什么这段代码会产生错误 wpf代码 <MediaElement x:Name="Player" LoadedBehavior="Manual"/> 产生的错误是 VideoPresenter.exe中发生“System.NullReferenceException”类型的异常,但未在用户代码中处理 附加信息:对象引用未设置为对象的实例。问题在于InitializeComponent()位于构造函数的末尾 Player.Source = new Uri(tempPath, UriK

为什么这段代码会产生错误

wpf代码

<MediaElement x:Name="Player" LoadedBehavior="Manual"/>
产生的错误是

VideoPresenter.exe中发生“System.NullReferenceException”类型的异常,但未在用户代码中处理


附加信息:对象引用未设置为对象的实例。

问题在于InitializeComponent()位于构造函数的末尾

Player.Source = new Uri(tempPath, UriKind.RelativeOrAbsolute);
Player.Play();
InitializeComponent();
InitializeComponent应该像这样在最上面

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

看起来
Player
为空。您必须首先创建一个实例。在该行设置一些断点并调试它。当它跳到该行时,尝试将鼠标悬停在
Player
上,它将被通知为
null
。确保Player元素初始化后执行Player的c#code。您的代码似乎在初始化Component()例程之前启动
InitializeComponent();
Player.Source = new Uri(tempPath, UriKind.RelativeOrAbsolute);
Player.Play();