我是新手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