0

I'am creating CAD application using WPF. And I want to implement render loop like following

public class Editor
{
    private DrawingGroup RenderGroup = new DrawingGroup();   

    protected override void OnRender(DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);
        var dc = drawingContext;
        Render();
        dc.DrawDrawing(RenderGroup);
    }

    void Render()
    {
         //All Render goes here, for example
         for (int i=0; i<25;i++)
         {
              dc.DrawRenctagle.....
         }    
    }

    public Editor()
    {
        CompositionTarget.Rendering += CompositionTarget_Rendering;
    }   

    void CompositionTarget_Rendering(object sender, EventArgs e)
    {
        Render();
    }
}

I have no experience in wpf looping, this way seems to be working for me now. But I am curious if I will run into troubles in future. Is there a "best practice" for tasks like that?

p.s Alternative to that is calling Render() only when it's need, like when I'am moving something on control and there is real need to redraw things. But in future I want to implement some animation features, and without loop it will be more challenging

  • Very bad, IMO. The Rendering event handler is called on every frame, i.e. up to 60 times per second. `OnRender` is usually called whenever necessary. Use [WPF Animations](https://learn.microsoft.com/en-us/dotnet/framework/wpf/graphics-multimedia/animation-overview) if you want to animate things. – Clemens May 15 '18 at 09:03
  • Did you noticed that I'm calling just Render. which actually rendering in drawing Group without calling OnrRender or Invalidate? This should make difference, am I wrong? – Alexandr Denschikov May 15 '18 at 10:05
  • Sure I did. However, you did not explain what exactly you're going to do there, and why that would not be achievable with regular animations. Anyway, "how bad" is asking for opinions. – Clemens May 15 '18 at 10:08
  • It's fine to use WPF for UI stuff like menu, buttons, panels, popup windows, etc. But I'd not try to write CAD-like software using WPF. It's way too [slow](https://stackoverflow.com/q/25450979/1997232). Not sure how good is 3d part, but you can always use bitmap to render stuff (using gdi+ or some libraries) and simply display the bitmap. No need for renderer. – Sinatr May 15 '18 at 10:11
  • I am drawing directly to drawingContext to have "more perfomance" instead of using Shape as childrens, therefore I can't use default storyboards because there is no triggers or properties or stuff like that. – Alexandr Denschikov May 15 '18 at 10:29
  • I think you'd be better off with child shapes and regular wpf transforms instead of redrawing yourself. But I also don't think you have to avoid rendertarget at all times in any case since it probably is pretty fast anyway. I have never had any significant perf problems with any of these methods. But if you use transforms instead of re-rendering (when possible) you will surely get a lot of perf benefits from that. – Andreas Zita May 15 '18 at 10:35
  • Sinatr, yeah, I looked into this example before, but in my case 1000 elements will be extreme case. – Alexandr Denschikov May 15 '18 at 10:40

0 Answers0