Windows Phone - Loss of simple pixel texture after resuming game

Hi,

I’ve been porting a game from Android to Windows Phone and now I’m running into the same issue we had over there - namely whenever you background the app and resume it, textures are lost - this is now happening on Windows Phone! I know this has been fixed for Android but it seems we now need the same fix for WP.

Some basic code that I’m using:

//decl
private Texture2D pixelTexture;

//in LoadContent()
pixelTexture = new Texture2D(GraphicsDevice, 1, 1, true, SurfaceFormat.Color);
pixelTexture.SetData(new[] { Color.White });

//in Draw()
Rectangle rectHealth = new Rectangle(30, 30, 100, 30);
_spriteBatch.Draw(pixelTexture, rectHealth, Color.Green * 0.9f);

Now all the above works perfectly - a rectangle is drawn to the screen. However, whenever you background the app - hit the Windows button or switch between an app and then resume the game, it never seems to draw pixelTexture again - nothing is drawn.

Furthermore, after the resume, if I try and do a pixelTexture.GetData() call, I get the following error:

{System.NullReferenceException: Object reference not set to an instance of an object.
at SharpDX.Direct3D11.Device.CreateTexture2D(Texture2DDescription& descRef, DataBox[] initialDataRef, Texture2D texture2DOut)
at SharpDX.Direct3D11.Texture2D…ctor(Device device, Texture2DDescription description)
at Microsoft.Xna.Framework.Graphics.Texture2D.GetData[T](Int32 level, Nullable1 rect, T[] data, Int32 startIndex, Int32 elementCount) at Microsoft.Xna.Framework.Graphics.Texture2D.GetData[T](T[] data) at Roids.App.Application_Deactivated(Object sender, DeactivatedEventArgs e) at System.EventHandler1.Invoke(Object sender, TEventArgs e)
at Microsoft.Phone.Shell.PhoneApplicationService.FireDeactivated(UInt32 reason)
at Microsoft.Phone.TaskModel.Interop.ITask.Pause.Invoke(UInt32 reason)
at Microsoft.Phone.TaskModel.Interop.Task.FireOnPause(UInt32 reason)}

Anyone have any ideas?

Many thanks,

Ian.

Sadly this is a limitation that can not be avoided (yet). You have to recreate all non-content manager resources upon resume.

Thanks Nezz for your reply.

How do I recreate the resources on resume? Can you give me an example? I’ve inspected all the variables after the resume and I get errors such as the ones I originally posted - namely in the GraphicsDevice.Handle, the context (reported by SharpDX) is set to NULL and whenever I try and recreate instances of any of the variables I get a NULL exception error.

Any ideas?

Thanks, Ian.

This is what we do:

private Texture2D texture;
private bool relaunch;

//In initialize:
this.Game.Activated += (sender, e) =>
{
    if (texture != null)
        relaunch = true;
};

//In draw:
if (relaunch)
{
    texture.SetData(new[] { Color.White });
    relaunch = false;
}

Nezz - you’re a star and a gentleman. This worked perfectly. I was attempting the same sort of thing but in different areas of the code and I was just getting muddled up as it wasn’t working.

Many, many thanks!

Ian.

It took me a while to figure it out, but now it’s working. Glad to hear that it’s working for you too :slight_smile: