Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.InvalidCastException: Unable to cast object of type ' XXXX' to type 'YYYY' #14453

Closed
sushuying521 opened this issue Nov 17, 2023 · 20 comments
Labels
kind/bug Something isn't working triage/needs-information Indicates an issue needs more information in order to work on it. triage/potentially-fixed Categorizes an issue as potentially fixed by some unlinked PR, fix needs to be verified triage/untriaged Indicates an issue requires triaging or verification

Comments

@sushuying521
Copy link

sushuying521 commented Nov 17, 2023

Current behavior

Hello, our project uses the c# language to develop the Uno framework + Gtk + Skia running on the Linux platform project,
and it is always triggered occasionally when InitializeComponent() is executed

System.InvalidCastException: Unable to cast object of type 'XXXXX' to type 'YYYYY'.””The following is an error message for one of them." ---> System.InvalidCastException: Unable to cast object of type 'System.Double' to type 'Windows.UI.Xaml.Input.XYFocusKeyboardNavigationMode'.
   at Windows.UI.Xaml.UIElement.OnXYFocusKeyboardNavigationBackingFieldUpdate(Object instance, Object newValue)
   at Windows.UI.Xaml.DependencyObjectStore.InvokeCallbacks(DependencyObject actualInstanceAlias, DependencyProperty property, DependencyPropertyDetails propertyDetails, Object previousValue, DependencyPropertyValuePrecedences previousPrecedence, Object newValue, DependencyPropertyValuePrecedences newPrecedence, Boolean bypassesPropagation)
   at Windows.UI.Xaml.DependencyObjectStore.RaiseCallbacks(DependencyObject actualInstanceAlias, DependencyPropertyDetails propertyDetails, Object previousValue, DependencyPropertyValuePrecedences previousPrecedence, Object newValue, DependencyPropertyValuePrecedences newPrecedence)
   at Windows.UI.Xaml.DependencyObjectStore.InnerSetValue(DependencyProperty property, Object value, DependencyPropertyValuePrecedences precedence, DependencyPropertyDetails propertyDetails, Boolean isPersistentResourceBinding)
   at Windows.UI.Xaml.DependencyObjectStore.OnParentPropertyChangedCallback(ManagedWeakReference sourceInstance, DependencyProperty parentProperty, Object newValue)
   at Windows.UI.Xaml.DependencyObjectStore.OnParentPropertyChangedCallback(ManagedWeakReference sourceInstance, DependencyProperty parentProperty, Object newValue)
   at Windows.UI.Xaml.DependencyObjectStore.<PropagateInheritedProperties>g__Propagate|151_0(DependencyObjectStore store, <>c__DisplayClass151_0&)
   at Windows.UI.Xaml.DependencyObjectStore.PropagateInheritedProperties(DependencyObjectStore childStore)
   at Windows.UI.Xaml.DependencyObjectStore.RegisterInheritedPropertyChangedCallback(DependencyObjectStore childStore)
   at Windows.UI.Xaml.DependencyObjectStore.RegisterInheritedProperties(IDependencyObjectStoreProvider parentProvider)
   at Windows.UI.Xaml.DependencyObjectStore.TryRegisterInheritedProperties(IDependencyObjectStoreProvider parentProvider, Boolean force)
   at Windows.UI.Xaml.DependencyObjectStore.set_Parent(Object value)
   at Windows.UI.Xaml.DependencyObjectExtensions.SetParent(IDependencyObjectStoreProvider storeProvider, Object parent)
   at Windows.UI.Xaml.UIElement.AddChild(UIElement child, Nullable`1 index)
   at Windows.UI.Xaml.Controls.Control.RegisterSubView(UIElement child)
   at Windows.UI.Xaml.Controls.Control.set_TemplatedRoot(UIElement value)
   at Windows.UI.Xaml.Controls.Control.UpdateTemplate()
   at Windows.UI.Xaml.Controls.Control.SetUpdateControlTemplate(Boolean forceUpdate)
   at Windows.UI.Xaml.Controls.Control.OnTemplateChanged(DependencyPropertyChangedEventArgs e)
   at Windows.UI.Xaml.Controls.Control.<>c.<.cctor>b__379_0(DependencyObject s, DependencyPropertyChangedEventArgs e)
   at Windows.UI.Xaml.DependencyObjectStore.InvokeCallbacks(DependencyObject actualInstanceAlias, DependencyProperty property, DependencyPropertyDetails propertyDetails, Object previousValue, DependencyPropertyValuePrecedences previousPrecedence, Object newValue, DependencyPropertyValuePrecedences newPrecedence, Boolean bypassesPropagation)
   at Windows.UI.Xaml.DependencyObjectStore.RaiseCallbacks(DependencyObject actualInstanceAlias, DependencyPropertyDetails propertyDetails, Object previousValue, DependencyPropertyValuePrecedences previousPrecedence, Object newValue, DependencyPropertyValuePrecedences newPrecedence)
   at Windows.UI.Xaml.DependencyObjectStore.InnerSetValue(DependencyProperty property, Object value, DependencyPropertyValuePrecedences precedence, DependencyPropertyDetails propertyDetails, Boolean isPersistentResourceBinding)
   at Windows.UI.Xaml.Setter.ApplyTo(DependencyObject o)
   at Windows.UI.Xaml.Style.ApplyTo(DependencyObject o, DependencyPropertyValuePrecedences precedence)
   at Windows.UI.Xaml.FrameworkElement.OnStyleChanged(Style oldStyle, Style newStyle, DependencyPropertyValuePrecedences precedence)
   at Windows.UI.Xaml.FrameworkElement.ApplyStyle()
   at Windows.UI.Xaml.FrameworkElement.ApplyStyles()
   at Windows.UI.Xaml.FrameworkElement.CreationComplete()
   at Uno.IntegrationTestPlatform.MyWindow.<>c.<InitializeComponent_44A45F2C>b__99_28(Button c18) in H:\Workflow\Vinno.Uno\Uno.IntegrationTestPlatform\obj\Release\netstandard2.0\g\XamlCodeGenerator\MyWindow_4a111db79bb1ee9bef1f3d2bdd7edb6c.g.cs:line 471"

Expected behavior

No response

How to reproduce it (as minimally and precisely as possible)

No response

Workaround

No response

Works on UWP/WinUI

None

Environment

Uno.UI / Uno.UI.WebAssembly / Uno.UI.Skia

NuGet package version(s)

",

Affected platforms

Skia (GTK on Linux/macOS/Windows)

IDE

Visual Studio 2022

IDE version

No response

Relevant plugins

No response

Anything else we need to know?

No response

@sushuying521 sushuying521 added difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification labels Nov 17, 2023
@Youssef1313
Copy link
Member

Can you provide a repro please?

@sushuying521
Copy link
Author

Xaml:

<Grid >
    <Popup x:Name="m_Popup" Height="72" Width="800">
        <Grid Background="#FF323232"  Width="800" Margin="72,0" MinHeight="72" >
        <TextBlock Margin="24,0"
                   HorizontalAlignment="Stretch"
                   VerticalAlignment="Center"
                   Text="{Binding StatusText}"
                   Foreground="White"
                   FontSize="{StaticResource TouchPanel.FontSize.Medium}"
                   TextWrapping="WrapWholeWords"
                   TextAlignment="Left" />
         </Grid>
    </Popup>
</Grid>
it is always triggered **occasionally** when InitializeComponent() is executed

@jeromelaban
Copy link
Member

Thanks. Make sure to provide all the details that are requested when creating a template, particularly the uno version that you're using.

@jeromelaban jeromelaban added the triage/needs-information Indicates an issue needs more information in order to work on it. label Nov 20, 2023
@MartinZikmund
Copy link
Member

Are you using XYFocusKeyboardNavigation anywhere within the code?

@MartinZikmund MartinZikmund removed the difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. label Nov 20, 2023
@MartinZikmund
Copy link
Member

@ebariche Could this be theoretically related to your changes?

@Youssef1313
Copy link
Member

@ebariche
Copy link
Member

@MartinZikmund I doubt so !

@jeromelaban
Copy link
Member

@ebariche this looks very similar to #14489. If people have been using the latest dev bits, though none of them have confirmed that they are, this would point to a change in dependency object.

@Youssef1313
Copy link
Member

Youssef1313 commented Nov 21, 2023

@jeromelaban I think some of the recent changes (specifically related to performance) assumed that certain code paths are only run in the UI thread.

Could it be something running in a different thread causing a race? In the stacktrace in the original post here, there is IntegrationTestPlatform in the stack trace. That would also explain why there is no 100% reliable repro.

@jeromelaban
Copy link
Member

It may, indeed. We do not have UI Thread checks in DependencyObject, but it may be interesting to enable them to validate if that's the issue.

@MartinZikmund
Copy link
Member

@sushuying521 Are you by any chance doing some asynchronous, non-UI thread work in your app by any chance?

@Youssef1313
Copy link
Member

#13963 might be related. It's one of those performance optimizations that rely on code properly executing in the UI thread.

If GetPropertyType is accessed by two different threads, bad things can happen.

@sushuying521
Copy link
Author

Thanks. Make sure to provide all the details that are requested when creating a template, particularly the uno version that you're using.

<PackageReference Include="Uno.UI" Version="4.9.17" />

@sushuying521
Copy link
Author

Are you using anywhere within the code?XYFocusKeyboardNavigation
This is not used in our code

@Youssef1313
Copy link
Member

@sushuying521 Are you able to upload a project where this happens?

@jeromelaban
Copy link
Member

If this is an error this is reported in 4.9.17 as well, this is likely not caused by recent changes. This may be caused by some unknown race condition.

@Youssef1313
Copy link
Member

@sushuying521 Do you have subscription to Unloaded event anywhere in your code?

@Youssef1313
Copy link
Member

Youssef1313 commented Nov 28, 2023

@sushuying521 Also, are you using any RadioButtonss in your code (not RadioButtons). I mean this:

public partial class RadioButtons : Control

In that case, #14599 might fix it.

@Youssef1313 Youssef1313 added triage/needs-information Indicates an issue needs more information in order to work on it. and removed triage/needs-information Indicates an issue needs more information in order to work on it. labels Nov 28, 2023
@Youssef1313
Copy link
Member

@ebariche @jeromelaban I think this should be fixed and can be closed?

@Youssef1313 Youssef1313 added the triage/potentially-fixed Categorizes an issue as potentially fixed by some unlinked PR, fix needs to be verified label Dec 19, 2023
@jeromelaban
Copy link
Member

Yes it is!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Something isn't working triage/needs-information Indicates an issue needs more information in order to work on it. triage/potentially-fixed Categorizes an issue as potentially fixed by some unlinked PR, fix needs to be verified triage/untriaged Indicates an issue requires triaging or verification
Projects
None yet
Development

No branches or pull requests

5 participants