

Likewise when the tab is activated again, Activated is fired. When a WpfGame is hosted inside a tab and the tab is changed, the WpfGame instance is not unloaded. The WpfGame does not unload in these cases. when switching to another tab) and fully reloads the tab when switching back. It is perfectly possible to use the WpfGame controls inside TabControls.īy default, WPF fully unloads any tab that is deactivated (e.g. The reason for this behaviour that the interop sample cannot use the backbuffer (null) and instead needs to use its own rendertarget to interop with WPF. _spriteBatch.Draw(this.rendertarget, Vector2.Zero, Color.White) GraphicsDevice.Clear(Color.CornflowerBlue) these draw calls will now render onto backbuffer GraphicsDevice.SetRenderTarget(wpfRenderTarget)

this will ensure that the output will end up visible to the user instead of setting null, set it back to the wpf rendertarget _spriteBatch.Draw(_texture, Vector2.Zero, Color.White) GraphicsDevice.SetRenderTarget(_rendertarget) Var wpfRenderTarget = (RenderTarget2D)GraphicsDevice.GetRenderTargets().RenderTarget get and cache the wpf rendertarget (there is always a default rendertarget) In a normal monogame the rendertarget would be used like this: Rendertargets work slightly different in this WPF interop. user holds and drags the mouse outside the window, then releases it -> game will still think the mouse is down until the window receives focus again) RenderTargets The downside is that mouse events outside the game window are no longer registered (e.g. textboxes on top of the game) will ever receive focus.Īlternatively this can be toggled off via CaptureMouseWithin property of WpfMouse and then allows focus on overlayed controls. The downside is that no overlayed controls (e.g. user holds and drags the mouse outside the window, then releases it -> game will still receive mouse up event). This allows capture of mouse events outside the game (e.g. This can be disabled via the FocusOnMouseOver property of WpfGame Mouse captureīy default the game captures the mouse.
ADDING MOUSE CODE TO MONOGAME VISUAL STUDIO UPDATE
every update we can now query the keyboard & mouse for our WpfGame var mouseState = _mouse. Protected override void Update( GameTime time) must be called after the WpfGraphicsDeviceService instance was created base.

wpf and keyboard need reference to the host control in order to receive input // this means every WpfGame control will have it's own keyboard & mouse manager which will only react if the mouse is in the control _keyboard = new WpfKeyboard( this) required by Content loading and rendering (will add itself to the Services) // note that MonoGame requires this to be initialized in the constructor, while WpfInterop requires it to // be called inside Initialize (before base.Initialize()) _graphicsDeviceManager = new WpfGraphicsDeviceService( this) IGraphicsDeviceService _graphicsDeviceManager
