Reputation: 177
Disclaimer: This problem occurred in Win10 1809, Win10 1909 test passed
I found a strange thing, I took a UIElement object (named polyline1
) from Canvas.Children, The full name of this object is Windows.UI.Xaml.Shapes.Polyline
, when I use (polyline1 as Polyline)?.ActualOffset
When I get the error mentioned in the title: Unable to cast object of type'Windows.UI.Xaml.Shapes.Polyline' to type'Windows.UI.Xaml.IUIElement10'
, even if I use polyline1.ActualOffset
I will get the same error.
Here is part of the code:
xml
<Canvas x:Name="canvas">
<Polyline x:Name="polyline1" Points="0,0 140,0 140,60" />
<Polyline x:Name="polyline2" Points="0,0 240,0 240,60" />
....
</Canvas>
code
try
{
var polyline1 = canvas.Children.FirstOrDefault(x => x.GetType().Name == typeof(Polyline).Name);
Debug.Write($"fullname: {polyline1.GetType().FullName}");
Debug.Write($"is polyline: {polyline1 is Polyline}");
Debug.Write($"as polyline: {polyline1 as Polyline}");
Debug.Write($"as polyline.ActualOffset.X: {(polyline1 as Polyline)?.ActualOffset.X}")
}
catch(Exception ex)
{
Debug.Write($"error: {ex}");
}
Output
fullname: Windows.UI.Xaml.Shapes.Polyline
is polyline: True
as polyline: Windows.UI.Xaml.Shapes.Polyline
error: System.InvalidCastException: Unable to cast object of type 'Windows.UI.Xaml.Shapes.Polyline' to type 'Windows.UI.Xaml.IUIElement10'.
at System.StubHelpers.StubHelpers.GetCOMIPFromRCW_WinRT(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget)
at Windows.UI.Xaml.UIElement.get_ActualOffset()
Upvotes: 0
Views: 130
Reputation: 32785
Is there any other way that can be equivalent to ActualOffset
Sure, you could use the TransformToVisual method to get the position of this UIElement, relative to its parent.
var ttv = polyline1.TransformToVisual(canvas);
Point position = ttv.TransformPoint(new Point(0, 0));
Upvotes: 1