简体   繁体   中英

c#, xna, wp7 touch screen not working

I am making a wp7 application and my touch screen doesnt seem to work. The code that i have wrote is below

protected override void Update(GameTime gameTime)
{
    TouchCollection touches = TouchPanel.GetState();
    if (touches.Count > 0 && touches[0].State == TouchLocationState.Pressed)
    {
        SpriteFont font = Content.Load<SpriteFont>("Font");
        Point touchPoint = new Point((int)touches[0].Position.X, (int)touches[0].Position.Y);
        spriteBatch.Begin();
        spriteBatch.DrawString(font, "hello ", new Vector2(touchPoint.X, touchPoint.Y), Color.White);
        spriteBatch.End();

        while (TouchPanel.IsGestureAvailable)
        {
            GestureSample gesture = TouchPanel.ReadGesture();
            switch (gesture.GestureType)
            {
                case GestureType.Tap:
                case GestureType.DoubleTap:
                    spriteBatch.Begin();
                    spriteBatch.DrawString(font, " ", new Vector2(touchPoint.X, touchPoint.Y), Color.White);
                    spriteBatch.End();
                    break;
            }
        }
    }
    base.Update(gameTime);
}

The problem can be that you trying to draw something in Update method. Draw method is for drawing . So in your case you draw something in Update method next you draw background in Draw method so you overried your text.

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);
    // overriding here
    ... draw background

    // *draw your text should be here
    base.Draw(gameTime);
}

So the solution is to move "spriteBatch.DrawString(...);" to Draw method and put it below background in your code or add depth parameter to draw methods.

looking at your code now, nothing seems wrong, but have you tried stepping through it? I see that tapping or double tapping do the same thing, but maybe something is being hung up.

The first thing that I thought of though, is your XAML. Do you have a control or something over top everything else? This could be catching you gestures.

If neither works, could you post more information?

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