How to retrieve Parent Visual Size within XamlCompositionBrushBase?

3

I'm creating a class inheriting from XamlCompositionBrushBase and want to know the render size of the visual the brush is attached to when it's hooked up in OnConnected.

I need this info to create a surface in my composition that's the size of my visual in order to not only have the right pixel density, but also map from the visual's coordinates into the surface's space.

i.e. if I have a coordinate 50, 75, I need to know the width/height of the visual to map the surface location to that location on the visual.

Since the brush is a dependency object, I tried using var parent = VisualTreeHelper.GetParent(this); but that threw an exception:

System.Exception occurred HResult=0x8000FFFF
Message=Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
Source=

Is this possible without using an attached property on the parent UIElement?

Thanks

uwp
winrt-xaml
windows-composition-api
xaml-composition
asked on Stack Overflow Feb 18, 2018 by Michael Hawker - MSFT • edited Feb 19, 2018 by Michael Hawker - MSFT

1 Answer

0

You cannot - because a brush can be connected to multiple visuals at once there's not really a concept of a "size". Well, not at this top layer of XAML in UWP anyway.

And as brushes can be used in resource dictionaries and hence applied as the same instance in multiple places each with different visual sizes, you really don't want to hack your way around this as you'll almost certainly break any usages of the brush in a resource dictionary.

(Which yes, is frustrating.)

answered on Stack Overflow Feb 27, 2018 by Johnny Westlake • edited Feb 27, 2018 by Johnny Westlake

User contributions licensed under CC BY-SA 3.0