C# xna中的声音问题

C# xna中的声音问题,c#,xna,C#,Xna,我有两个主页,GamePage(游戏的所有代码都在其中)和Mainpage(标题屏幕)。我希望能够在标题屏幕上播放音乐(mainpage.xaml) 我尝试过使用content.load方法,就像我在gamepage.xaml中所做的那样。但这不起作用,它产生了一个null异常。我也试着把它放在题为mazeescapephonesilverlight的屏幕截图中,但是当我试着运行它时;声音文件中出现错误意外字符 如何在主页上播放音乐.xaml,我尝试过的似乎都没有: Codebehindmain

我有两个主页,
GamePage
(游戏的所有代码都在其中)和
Mainpage
(标题屏幕)。我希望能够在标题屏幕上播放音乐(
mainpage.xaml

我尝试过使用
content.load
方法,就像我在
gamepage.xaml
中所做的那样。但这不起作用,它产生了一个
null异常
。我也试着把它放在题为
mazeescapephonesilverlight
的屏幕截图中,但是当我试着运行它时;声音文件中出现错误
意外字符

如何在
主页上播放音乐.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));
    }
public部分类主页:PhoneApplicationPage
{
//建造师
ContentManager;
MediaElement播放器=null;
Popup nameEntry=新的Popup();
布尔游戏音乐;
bool-mazeStart=true;
TextBox txtnamentry=new TextBox();
音效主题音乐;
SoundEffectInstance主题\音乐\实例;
声效zap;
健全的立场;
公共主页()
{
初始化组件();
//SharedGraphicsDeviceManager.Current.GraphicsDevice.SetShareingMode(true);
//通过mediaplayer和公共布尔值播放音乐。
if(App.Current.Resources.Contains(“mediaPlayer”))
{
player=App.Current.Resources[“mediaPlayer”]作为MediaElement;
player.Source=newURI(“./mazeescapephonesilverlightlibcontent/Media/magic_wav_file”,UriKind.Relative);
}
gameMusic=((App)Application.Current).gameMusic;
if(gameMusic)player.Play();
}
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
if(((App)Application.Current.gameMusic)
{
//theme_music=this.contentManager.Load(“媒体/magic_wav_文件”);
//theme_music_Instance=theme_music.CreateInstance();
}
}
//方法在按下“开始游戏”按钮时运行
私有void btnStartGame_单击(对象发送者,路由目标e)
{
if(游戏音乐)
{
player.Play();
}
#区域创建和显示弹出窗口
边框=新边框();
border.BorderBrush=新的SolidColorBrush(Colors.White);
border.BorderThickness=新厚度(10);
StackPanel myStackPanel=新StackPanel();
myStackPanel.Background=新的SolidColorBrush(Colors.Black);
//包含您输入的名称及其属性的Textblock。
//txtNameEntry.Text=Microsoft.Phone.Info.DeviceExtendedProperties.GetValue(“DeviceName”).ToString();
txtnamentry.Text=“播放器1”;
txtnamentry.Width=350;
txtnamentry.Height=100;
txtNameEntry.MaxLength=10;
txtNameEntry.FontFamily=新FontFamily(“Comis Sans MS”);
txtNameEntry.FontSize=48.0;
txtnamentry.Foreground=新的SolidColorBrush(Colors.Orange);
txtnamentry.Background=新的SolidColorBrush(Colors.LightGray);
txtnamentry.BorderBrush=新的SolidColorBrush(Colors.LightGray);
//确定按钮,然后允许您进入游戏。
按钮btnNameEntryOK=新按钮();
btnNameEntryOK.Content=“确定”;
btnNameEntryOK.Background=新的SolidColorBrush(Colors.Orange);
btnNameEntryOK.fontflamily=新的fontflamily(“Comic Sans Ms”);
btnNameEntryOK.FontSize=25.0;
Btnameentryok.宽度=180;
Btnameentryok.高度=70;
btnNameEntryOK.Click+=新建路由EventHandler(btnNameEntryOK\u Click);
Btnameentryok.余量=新厚度(10);
//按照您希望它们渲染到屏幕的顺序放置它们。
myStackPanel.Children.Add(txtnamentry);
myStackPanel.Children.Add(btnNameEntryOK);
border.Child=myStackPanel;
namentry.Child=边框;
//设置弹出窗口的屏幕位置
nameEntry.VerticalOffset=100.0;
nameEntry.HorizontalOffset=50.0;
//打开弹出窗口
nameEntry.IsOpen=true;
#端区
if(nameEntry.IsOpen==false&&mazeStart==true)
{
player.Stop();
}
}
无效Btnameentryok_单击(对象发送者,路由目标e)
{
((App)Application.Current).playerName=txtnamentry.Text;
nameEntry.IsOpen=false;
mazeStart=true;
player.Stop();
NavigationService.Navigate(新Uri(“/GamePage.xaml”,UriKind.Relative));
}
私有无效b选项\u单击(对象发送方,路由目标)
{
nameEntry.IsOpen=false;
NavigationService.Navigate(新Uri(“/Options.xaml”,UriKind.Relative));
}
解决方案资源管理器的屏幕截图:


您是否已使用调试器对此进行了调试,以确保满足条件,并按照预期进行初始化?此
gameMusic=((App)Application.Current).gameMusic
看起来有点可疑,你确定它的计算结果是
真的吗
?是的,它在代码中运行良好,我在visual studio 2010中使用过它,但在2012年它不起作用可能是2010年和2012年之间的交叉问题,只是想一想,XNA我相信有一些问题