monogame window is flickering black during runtime

my monogame window is flickering black during runtime
my cpu is i7-11800H with RTX3050
i am using monogame wpf interop to host monogame in my wpf app
any way to fix this ?

namespace View3D
{
    public class GameWorld : WpfGame 
    {
        private bool _disposed;
        WpfGraphicsDeviceService _deviceServiceHandle;
        private IGraphicsDeviceService _graphicsDeviceManager;
        private GraphicsDeviceManager _graphics;
       
        LightControllerComponent _lights;
        public ArcBallCamera _camera;
        public MouseComponent _mouseComponent;
        public KeyboardComponent _keyboardComponent;

        public BasicEffect basicEffect;

         public SelectionComponent selectionComponent;


        public  Matrix WorldMatrix = Matrix.CreateWorld(Vector3.Zero, Vector3.Forward, Vector3.Up);
          SpriteBatch spriteBatch;
        Rectangle IndicatorArea;
         public   bool IsCameraOwner = false;
        bool IsMouseLocked = false;
        Vector2 IndicatorAreaCinter;

        public Vector2 ViewCinter { get; private set; }

        public RenderFormats shaderType { get;  set; } = RenderFormats.MetalRoughness;
        GridComponent ground ;

      
        public double FrameRate { get;  set; } = 30;
        Texture2D Backgroundshade;
      
        Texture2D axistexture;
        public Color MegaGray = new Color(59, 59, 59, 255);
        
        PbrShader_MetalRoughness MetalRough;
        public CommonShaderParameters commonShaderParameters;
        
      
        public RenderEngineComponent renderEngineComponent;
        public ResourceLibary resourceLibary;
        public DeviceResolverComponent _deviceResolverComponent;

        public float EnvLightRotationDegrees_Y { get; set; } = 20;
        public float DirLightRotationDegrees_X { get; set; } = 0;
        public float DirLightRotationDegrees_Y { get; set; } = 0;
        public float LightIntensityMult { get; set; } = 1;



        public  List<BoneNodeDif> DrawableBones = new List<BoneNodeDif>();
        public  VertexPositionColor[] Dummies = new VertexPositionColor[0];

        public RenderEnvironment renderEnvironment;
        SelectionManager selectionManager;
        private EventHub EventHub;
        public GizmoComponent gizmo;
        CommandExecutor commandExecutor;

        ObjectSelectionCommand objectSelectionCommand;

        public GameWorld(EventHub eventHub, string contentDir = "BuiltContent") : base(eventHub, contentDir)
        {
            EventHub = eventHub;
        }

        protected override void Initialize()
        {
            _disposed = false;
            _deviceServiceHandle = new WpfGraphicsDeviceService(this);
            _deviceResolverComponent = new DeviceResolverComponent(this);
            _mouseComponent = new MouseComponent(this);
            _keyboardComponent = new KeyboardComponent(this);
            _camera = new ArcBallCamera(_deviceResolverComponent, _keyboardComponent, _mouseComponent);
            
            Content = new ContentManager(this.Services, "BuiltContent");
            resourceLibary = new ResourceLibary(this);
            renderEngineComponent = new RenderEngineComponent(_camera, resourceLibary, _deviceResolverComponent);
            renderEngineComponent.MainRenderFormat = shaderType;
            _lights = new LightControllerComponent(resourceLibary, _deviceResolverComponent, _keyboardComponent, renderEngineComponent);
            selectionManager = new SelectionManager(EventHub, renderEngineComponent, resourceLibary,_deviceResolverComponent);
            commandExecutor = new(EventHub);

            spriteBatch = resourceLibary.CommonSpriteBatch;
            basicEffect = new BasicEffect(GraphicsDevice);
            //   vertexInstanceMesh = new VertexInstanceMesh(_deviceResolverComponent, resourceLibary);
            objectSelectionCommand = new ObjectSelectionCommand(selectionManager);
          



            selectionComponent = new(_mouseComponent, _keyboardComponent, _camera, selectionManager, _deviceResolverComponent);




           

            loadEnvironment();


            base.Initialize();
        }
        public void loadEnvironment()
        {
            renderEnvironment = new RenderEnvironment(_camera, resourceLibary, _deviceResolverComponent);
            GraphicsCardGeometry CardGeometry = new GraphicsCardGeometry(GraphicsDevice);
            MeshObject mesh = SimpleCubeMesh.getmesh(CardGeometry, 20);

            EnvironmentGeo envirnmentmesh = new EnvironmentGeo(GraphicsDevice, resourceLibary, mesh, SimpleCubeMesh.GetMaterial(), renderEnvironment);
            envirnmentmesh.Render(renderEnvironment, WorldMatrix);



        }
       
        protected override void LoadContent()
        {
            
            
            base.LoadContent();
        }
        protected override void Update(GameTime gameTime)
        {
            _mouseComponent.Update(gameTime);
            _keyboardComponent.Update(gameTime);
            

            IndicatorArea = new Rectangle { X = GraphicsDevice.Viewport.Width - 90, Y = 60, Height = 80, Width = 80 };
            IndicatorAreaCinter = new Vector2(GraphicsDevice.Viewport.Width - 90, 60);
            ViewCinter = new Vector2(GraphicsDevice.Viewport.Width/2, GraphicsDevice.Viewport.Height/2);

          

            if (_mouseComponent.IsMouseButtonDown(MouseButton.Left)||  _mouseComponent.IsMouseButtonDown(MouseButton.Right))
            {
                IsCameraOwner = true;
                _mouseComponent.IsCursorVisible = false;


            }
            else
            {
                IsCameraOwner = false;
                _mouseComponent.IsCursorVisible = true;

            }
            

            _camera.Update(gameTime);
            _lights.Update(gameTime);
            selectionManager.Update(gameTime);
            selectionComponent.Update(gameTime);
            base.Update(gameTime);
        }
        protected override void Draw(GameTime time)
        {
            GraphicsDevice.Clear(MegaGray);
            renderEnvironment.Draw(time);

            base.Draw(time);

           

        }
       
      
       
    }

  
   
}

Where did found the WpfGraphicsDeviceService class?

Clearly it’s a problem with syncing the shared surface. It get’s cleaned and drawn over before wpf composition have the time to draw the last complete texture. I’ve tried several libraries out there but they all seem to have problems like this, so of cource I wrote my own that works.

1 Like

i used the assets from this repo