我有两个主要页面,GamePage
(玩游戏的所有代码都在Mainpage
其中)和(标题屏幕)。我想做的是在标题屏幕上播放音乐 ( mainpage.xaml
)
我尝试过使用content.load
我在gamepage.xaml
. 但这不起作用,它提出了一个null exception
. 我也试过把它放在你可以在截图中看到的部分MazeEscapePhoneSilveright
,但是当我尝试运行它时;错误unexpected characters in the sound file
出现。
我怎样才能在 上播放音乐mainpage.xaml
,我没有尝试过似乎:
代码隐藏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));
}
解决方案资源管理器的屏幕截图: