Render something once in multiple templates
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
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
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: Render something once in multiple templates
Hi Steve, you can use a VisualBrush to render the spinning widget once, and then reuse it as a brush in several controls:
Re: Render something once in multiple templates
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
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
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: Render something once in multiple templates
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.
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.
Re: Render something once in multiple templates
That works perfect! Cheers for the help.
Who is online
Users browsing this forum: No registered users and 36 guests