我有两个主要页面GamePage
(游戏中所有代码都在其中)和Mainpage
(标题屏幕)。我想要做的是在标题画面上播放音乐(mainpage.xaml
)
我尝试使用content.load
方法,就像我在gamepage.xaml
中所做的那样。但这不起作用,它提出null exception
。我也尝试将其放在标题为MazeEscapePhoneSilveright
的屏幕截图中,但是当我尝试运行它时;错误unexpected characters in the sound file
出现了。
如何在mainpage.xaml
上播放音乐,似乎没有尝试过:
Codebehind mainpage.xaml
:
public partial class MainPage : PhoneApplicationPage
{
// Constructor
ContentManager contentManager;
MediaElement player = null;
Popup nameEntry = new Popup();
bool gameMusic;
bool mazeStart = true;
TextBox txtNameEntry = new TextBox();
SoundEffect theme_music;
SoundEffectInstance theme_music_Instance;
SoundEffect zap;
SoundEffectInstance zapInstance;
public MainPage()
{
InitializeComponent();
//SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);
//Plays the music through a mediaplayer, and a public boolean value.
if (App.Current.Resources.Contains("mediaPlayer"))
{
player = App.Current.Resources["mediaPlayer"] as MediaElement;
player.Source = new Uri("./MazeEscapePhoneSilverightLibContent/Media/magic_wav_file", UriKind.Relative);
}
gameMusic = ((App)Application.Current).gameMusic;
if (gameMusic) player.Play();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (((App)Application.Current).gameMusic)
{
//theme_music = this.contentManager.Load<SoundEffect>("Media/magic_wav_file");
//theme_music_Instance = theme_music.CreateInstance();
}
}
//method runs when "Start Game" button is pressed
private void btnStartGame_Click(object sender, RoutedEventArgs e)
{
if (gameMusic)
{
player.Play();
}
#region CREATE AND DISPLAY POP UP
Border border = new Border();
border.BorderBrush = new SolidColorBrush(Colors.White);
border.BorderThickness = new Thickness(10);
StackPanel myStackPanel = new StackPanel();
myStackPanel.Background = new SolidColorBrush(Colors.Black);
//Textblock containing the name you input and its properties.
//txtNameEntry.Text = Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("DeviceName").ToString();
txtNameEntry.Text = "Player 1";
txtNameEntry.Width = 350;
txtNameEntry.Height = 100;
txtNameEntry.MaxLength = 10;
txtNameEntry.FontFamily = new FontFamily("Comis Sans MS");
txtNameEntry.FontSize = 48.0;
txtNameEntry.Foreground = new SolidColorBrush(Colors.Orange);
txtNameEntry.Background = new SolidColorBrush(Colors.LightGray);
txtNameEntry.BorderBrush = new SolidColorBrush(Colors.LightGray);
//The ok button, which then allows you to procede into the game.
Button btnNameEntryOK = new Button();
btnNameEntryOK.Content = "Ok";
btnNameEntryOK.Background = new SolidColorBrush(Colors.Orange);
btnNameEntryOK.FontFamily = new FontFamily("Comic Sans Ms");
btnNameEntryOK.FontSize = 25.0;
btnNameEntryOK.Width = 180;
btnNameEntryOK.Height = 70;
btnNameEntryOK.Click += new RoutedEventHandler(btnNameEntryOK_Click);
btnNameEntryOK.Margin = new Thickness(10);
//Place these in the order you want them to be renderd to the screen.
myStackPanel.Children.Add(txtNameEntry);
myStackPanel.Children.Add(btnNameEntryOK);
border.Child = myStackPanel;
nameEntry.Child = border;
//set screen position of pop up
nameEntry.VerticalOffset = 100.0;
nameEntry.HorizontalOffset = 50.0;
//open pop up
nameEntry.IsOpen = true;
#endregion
if (nameEntry.IsOpen == false && mazeStart == true)
{
player.Stop();
}
}
void btnNameEntryOK_Click(object sender, RoutedEventArgs e)
{
((App)Application.Current).playerName = txtNameEntry.Text;
nameEntry.IsOpen = false;
mazeStart = true;
player.Stop();
NavigationService.Navigate(new Uri("/GamePage.xaml", UriKind.Relative));
}
private void btnOptions_Click(object sender, RoutedEventArgs e)
{
nameEntry.IsOpen = false;
NavigationService.Navigate(new Uri("/Options.xaml", UriKind.Relative));
}
解决方案资源管理器的屏幕截图: