我是新手C#程序员,并且在使用VS 2008的WPF(Windows)应用程序中播放音乐时遇到问题。这是一个Web应用程序。我认为正在发生的是myMediaElementExample
变量在用于执行ExpenseReportPage.xaml.cs
文件中的Play
方法时为空。如何在WPF中播放声音
现在这个程序建立了,但是在我运行它之后,它在myMediaElementExample.Play();
行处遇到异常。例外情况如下:
An unhandled win32 exception occurred in the WpfApplication1.vhost.exe [948].
你们能给我提示我还有什么可以尝试吗?我只包含有关这一问题的代码:
ExpenseReportPage.xaml.cs文件:
namespace ExpenseIt
{
public partial class ExpenseReportPage : Page
{
... }
public partial class MediaElementExample : Page
{
MediaElement myMediaElementExample = new MediaElement();
public MediaElementExample()
{
}
public void OnMouseDownPlayMedia(object sender, RoutedEventArgs args) //MouseButtonEventArgs
{
// The Play method will begin the media if it is not currently active or
// resume media if it is paused. This has no effect if the media is
// already running.
myMediaElementExample.Play();
}
}
}
HomePage.xaml.cs文件:
namespace ExpenseIt
{
public partial class HomePage : Page
{
MediaElementExample mediaElementExample = new MediaElementExample();
public HomePage()
{
InitializeComponent();
}
void HandleClick(object sender, RoutedEventArgs e)
{
Button srcButton = e.Source as Button;
srcButton.Width = 200;
mediaElementExample.OnMouseDownPlayMedia(sender, e);
}
}
}
哇,我期待着一个简单的。 –
你在哪里设置媒体源,即播放的mp3/wav? – kiwipom