steveh
Topic Author
Posts: 42
Joined: 07 Oct 2019, 12:50

Render something once in multiple templates

24 Jun 2022, 01:18

Hi guys,

So I have a custom ControlTemplate which references a "loading" spinning widget so we can show a placeholder XAML object until some objects have streamed in. I've created a smaller sample that sort of represents what we have:

https://www.noesisengine.com/xamltoy/5f ... 25059228f7

In that example, you can see we have 2 "Common_Widget" controls. The ControlTemplate of that references a "Spinning_Widget" control. Right now, that Spinning_Widget will be duplicated for each instance of the Common_Widget. Ideally what I want is to have 1 global instance of a Spinning_Widget, and simply reference that in the Common_Widget so we end up with:

- 1 Spinning_Widget which is just ticking each frame and just constantly animates
- 2 Common_Widgets - each simply rendering the Spinning_Widget.

Is there anything we can do to solve this directly within XAML? The only solution I can think of is to create a separate XAML file, update that on a separate view, render that view to a different render target (off-screen render target), then bind that texture using a TextureSource.

Is there any cheaper / quicker way to do this?

Cheers,

-Steven
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: Render something once in multiple templates

27 Jun 2022, 10:37

Hi Steve, you can use a VisualBrush to render the spinning widget once, and then reuse it as a brush in several controls:

 
steveh
Topic Author
Posts: 42
Joined: 07 Oct 2019, 12:50

Re: Render something once in multiple templates

29 Jun 2022, 00:15

Much appreciated Sergio, that's pretty much perfect!

However, there is a slight glitch when the brush is slightly more complicated. I've not worked out the cause of it yet, but I'm guessing it may have something to do with the viewbox.
You can see the glitch here:

https://www.noesisengine.com/xamltoy/b1 ... 9cc3abd23a

Notice the original at the top works fine, but the brush has a weird scale glitch. I'm guessing the bounds are being messed up by either the rotation or the viewbox and then it thinks it needs to apply a scale.

-Steven
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: Render something once in multiple templates

29 Jun 2022, 23:43

Hi Steve, that is happening because the VisualBrush takes into account the bounding box of the target visual to generate the brush texture, and when the inner controls are rotated the bbox gets bigger (you can clearly see that if you set the Background of one of the Canvas to Yellow), this is the same WPF does.

You can wrap the rotating elements with a container that has enough room so the bbox is not altered. I updated my xamltoy to show that.



P.S. All those paths, as they share the same brush, can be combined into a single path in Blend by selecting them, right click, Path > Make Compound Path.
 
steveh
Topic Author
Posts: 42
Joined: 07 Oct 2019, 12:50

Re: Render something once in multiple templates

30 Jun 2022, 01:21

That works perfect! Cheers for the help.

Who is online

Users browsing this forum: Google [Bot] and 88 guests