After working on some Windows Phone apps that required the usage of a DrawingSurface/DrawingSurfaceBackgroundGrid to perform Direct3D drawing, I have isolated some of the code in a standalone library for easily initialization of the Direct3D device, context and associated resources.
The library abstracts the initialization of Direct3D and transparently handles such cases as the control being resized (for example, a Windows Store application being snapped) and the device being lost or recreated (Windows Phone does this when sending the application to the foreground). The only dependency comes from SharpDX and it can be used in Windows Phone 8, Windows Phone 8.1 Silverlight, Windows Store 8.1 and Universal projects.
This is the workflow you should follow when using it:
- When navigating to a page where the Direct3D drawing will be performed, instantiate a SharpDXContext object.
- Subscribe to its DeviceReset and Render events to perform resource loading and drawing, respectively.
- Call BindToControl passing the existing DrawingSurface, DrawingSurfaceBackgroundGrid, SwapChainPanel or SwapChainBackgroundPanel control.
- The event Render will be raised whenever the control asks for a new frame, perform your drawing there using the D3DDevice and D3DContext properties.
- When navigating away from the page, unsubscribe from the events and Dispose the SharpDXContext object.
You can download the library from NuGet, or take a look at the code on GitHub. Some sample projects that show how to properly manage the lifetime of the SharpDXContext object are available here.