为什么我在另一个类中检查媒体元素源时会得到空引用异常?

时间:2018-01-23 12:56:42

标签: c# wpf nullreferenceexception mediaelement

我试图运行一个迷你程序,当我点击按钮时能够播放收音机。

private void Yes933_Click(object sender, RoutedEventArgs e)
{
    if (Player.Source != null)
    {
        Player.LoadedBehavior = MediaState.Manual;
        Player.Stop();
        Player.Source = null;
    }
        Player.LoadedBehavior = MediaState.Manual;
        string StationUrl = "http://www.radiosingapore.org/#radio-y-e-s-93-3-fm";
        Player.Source = new Uri(StationUrl, UriKind.RelativeOrAbsolute);
        Player.Play();
}

如果我点击"下一个"按钮,它应该播放下一个频道。

private void NextStation_Click(object sender, RoutedEventArgs e)
{
    robot.Nextstation();
}

这是另一个类中的Nextstation()方法:

public void Nextstation()
{
    if (mainWindows.Player.Source != null)
    {
        string StationUrl;
        switch (mainWindows.Player.Source.AbsoluteUri)
        {
            case "http://www.radiosingapore.org/#radio-class-95-fm":
            StationUrl = "http://www.radiosingapore.org/#radio-y-e-s-93-3-fm";
            break; 
            case "http://www.radiosingapore.org/#radio-y-e-s-93-3-fm":
            StationUrl = "http://www.radiosingapore.org/#radio-class-95-fm";
            break;
            default:
            StationUrl = "http://www.radiosingapore.org/#radio-class-95-fm";
            break;
         }
         mainWindows.Player.LoadedBehavior = MediaState.Manual;
         mainWindows.Player.Stop();
         mainWindows.Player.Source = null;
         mainWindows.Player.LoadedBehavior = MediaState.Manual;
         mainWindows.Player.Source = new Uri(StationUrl, UriKind.RelativeOrAbsolute);
         mainWindows.Player.Play();
    }

}

但它显示: enter image description here

我不知道为什么会这样。有什么建议吗?提前谢谢。

0 个答案:

没有答案