question

Victor-Chen avatar image
0 Votes"
Victor-Chen asked Victor-Chen commented

Unable to cast object of type 'Windows.UI.Xaml.Shapes.Polyline' to type 'Windows.UI.Xaml.IUIElement10'

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()









windows-uwp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

hi VictorChen, I found you have posted the same case in stackoverflow, I have added the comment there. And I will follow there, please pay attention to the following updates

1 Vote 1 ·

Hi @NicoZhu-MSFT, why not the opposite?


0 Votes 0 ·

1 Answer

danielescipioni avatar image
0 Votes"
danielescipioni answered Victor-Chen commented

As you can see in the documentation here https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.uielement.actualoffset, UIElement.ActualOffset was added with 10.0.18362.0 (1903). If you use TargetPlatformVersion is greater or equals to 10.0.18362.0, you are allowed to use in your code UIElement.ActualOffset and there are no compilation errors, but if your TargetPlatformMinVersion is smaller than 10.0.18362.0, than you have to add to your code extra checks before to use to use it.

Use ApiInformation method to detect whether a specified member, type, or API contract is present, for example:

try
{
    UIElement 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}");
    bool isPropertyPresent = ApiInformation.IsPropertyPresent("Windows.UI.Xaml.UIElement", "ActualOffset");
    double actualOffsetX = isPropertyPresent
        ? polyline1.ActualOffset.X
        : polyline1.TransformToVisual(canvas).TransformPoint(new Point(0, 0)).X;
}
catch (Exception ex)
{
    Debug.Write($"error: {ex}");
}


The example above is just to show how you can use ApiInformation, in your particular case I think the best option is to use only

polyline1.TransformToVisual(canvas).TransformPoint(new Point(0, 0)).X;
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you very much! You really know everything!

0 Votes 0 ·