简体   繁体   中英

WP7 Silverlight/XNA split

I want to make an app that has Silverlight menus but the game part of the app is XNA. I am trying to get the Silverlight/XNA split working using the code from this example game and the method of doing XNA rendering in Silverlight here . Combining these 2 tutorials I have source code that looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;

namespace FYP
{
    public partial class GamePage : PhoneApplicationPage
    {
        GameTimer timer;
        SpriteBatch spriteBatch;
        Texture2D ballTexture;
        IList<Ball> balls = new List<Ball>();
        bool touching = false;

        public GamePage(ContentManager contentManager)
        {
            InitializeComponent();

            //base.Initialize();

            // Create a timer for this page
            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromTicks(333333);
            //timer.Update += OnUpdate;
            //timer.Draw += OnDraw;

            // TODO: use this.Content to load your game content here
            ballTexture = contentManager.Load<Texture2D>("Ball");
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            // Set the sharing mode of the graphics device to turn on XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

            timer.Start();

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            // Set the sharing mode of the graphics device to turn off XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);

            // Stop the timer
            timer.Stop();
        }

        private void OnUpdate(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                //this.Exit();

            // TODO: Add your update logic here

            //base.Update(gameTime);

            HandleTouches();
            UpdateBalls();
        }

        private void OnDraw(GameTime gameTime)
        {
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.White);

            // TODO: Add your drawing code here
            foreach (Ball ball in balls)
            {
                ball.Draw(spriteBatch);
            }

            //base.Draw(gameTime);
        }

        private void HandleTouches()
        {
            TouchCollection touches = TouchPanel.GetState();
            if (!touching && touches.Count > 0)
            {
                touching = true;
                Random random = new Random(DateTime.Now.Millisecond);
                Color ballColor = new Color(random.Next(255), random.Next(255), random.Next(255));
                Vector2 velocity = new Vector2((random.NextDouble() > .5 ? -1 : 1) * random.Next(9), (random.NextDouble() > .5 ? -1 : 1) * random.Next(9)) + Vector2.UnitX + Vector2.UnitY;
                //Vector2 center = new Vector2((float)SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width / 2, (float)SharedGraphicsDeviceManager.Current.GraphicsDevice.Height / 2);
                Vector2 center = new Vector2((float)SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width / 2, 200);
                float radius = 25f * (float)random.NextDouble() + 5f;
                balls.Add(new Ball(this, ballColor, ballTexture, center, velocity, radius));
            }
            else if (touches.Count == 0)
            {
                touching = false;
            }
        }

        private void UpdateBalls()
        {
            foreach (Ball ball in balls)
            {
                ball.Update();
            }
        }
    }
}

I do not understand how base works, I've had to comment out base.initialize, update and draw however base.OnNavigatedFrom works.

Also, should I be able to get my code to work in theory? I find it very complicated and although reading about XNA/Silverlight to be possible I can't find any source code where people have successfully combined XNA and Silverlight into the same app.

these videos will help you out to understand XNA and SilverLight/XNA combined platform

http://channel9.msdn.com/Series/Mango-Jump-Start/Mango-Jump-Start-11a-XNA-for-Windows-Phone--Part-1

http://channel9.msdn.com/Series/Mango-Jump-Start/Mango-Jump-Start-11b-XNA-for-Windows-Phone--Part-2

Theoretically XNA and SilverLight XNA Combined platform are pretty much the same, just a difference of bits and pieces, You can even ask XNA to render some SilverLight Control, which will make it easier to handle some button event in your game.

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM