XAML fails to load (11.2)
Describe the bug
Probably after https://github.com/AvaloniaUI/Avalonia/commit/a7e102676f5854274f246ee82e78b3a3d7f80a30, my app stopped working upon xaml loading:
---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.InvalidCastException: Specified cast is not valid.
at CompiledAvaloniaXaml.XamlDynamicSetters.<>XamlDynamicSetter_23(SolidColorBrush, Object)
at Avalonia.Themes.Fluent.FluentTheme.XamlClosure_3.Build_6(IServiceProvider)
at Avalonia.Markup.Xaml.XamlIl.Runtime.XamlIlRuntimeHelpers.PointerDeferredContent`1.InvokeBuilder(IServiceProvider serviceProvider)
at Avalonia.Markup.Xaml.XamlIl.Runtime.XamlIlRuntimeHelpers.DeferredContent`1.Build(IServiceProvider serviceProvider)
at Avalonia.Controls.ResourceDictionary.TryGetValue(Object key, Object& value)
at Avalonia.Controls.ResourceDictionary.TryGetResource(Object key, ThemeVariant theme, Object& value)
at Avalonia.Controls.ResourceDictionary.TryGetResource(Object key, ThemeVariant theme, Object& value)
at Avalonia.Styling.Styles.TryGetResource(Object key, ThemeVariant theme, Object& value)
at Avalonia.Themes.Fluent.FluentTheme.Avalonia.Controls.IResourceNode.TryGetResource(Object key, ThemeVariant theme, Object& value)
at Avalonia.Styling.Styles.TryGetResource(Object key, ThemeVariant theme, Object& value)
at Avalonia.Markup.Xaml.Styling.StyleInclude.TryGetResource(Object key, ThemeVariant theme, Object& value)
at Avalonia.Styling.Styles.TryGetResource(Object key, ThemeVariant theme, Object& value)
at Avalonia.Application.TryGetResource(Object key, ThemeVariant theme, Object& value)
at Avalonia.Markup.Xaml.MarkupExtensions.StaticResourceExtension.ProvideValue(IServiceProvider serviceProvider)
at Avalonia.Themes.Fluent.FluentTheme.XamlClosure_3.Build_268(IServiceProvider)
at Avalonia.Markup.Xaml.XamlIl.Runtime.XamlIlRuntimeHelpers.PointerDeferredContent`1.InvokeBuilder(IServiceProvider serviceProvider)
at Avalonia.Markup.Xaml.XamlIl.Runtime.XamlIlRuntimeHelpers.DeferredContent`1.Build(IServiceProvider serviceProvider)
at Avalonia.Controls.ResourceDictionary.TryGetValue(Object key, Object& value)
at Avalonia.Controls.ResourceDictionary.TryGetResource(Object key, ThemeVariant theme, Object& value)
at Avalonia.Controls.ResourceDictionary.TryGetResource(Object key, ThemeVariant theme, Object& value)
at Avalonia.Styling.Styles.TryGetResource(Object key, ThemeVariant theme, Object& value)
at Avalonia.Themes.Fluent.FluentTheme.Avalonia.Controls.IResourceNode.TryGetResource(Object key, ThemeVariant theme, Object& value)
at Avalonia.Styling.Styles.TryGetResource(Object key, ThemeVariant theme, Object& value)
at Avalonia.Markup.Xaml.Styling.StyleInclude.TryGetResource(Object key, ThemeVariant theme, Object& value)
at Avalonia.Styling.Styles.TryGetResource(Object key, ThemeVariant theme, Object& value)
at Avalonia.Application.TryGetResource(Object key, ThemeVariant theme, Object& value)
at Avalonia.Controls.ResourceNodeExtensions.TryFindResource(IResourceHost control, Object key, ThemeVariant theme, Object& value)
at Avalonia.Controls.ResourceNodeExtensions.FindResource(IResourceHost control, ThemeVariant theme, Object key)
at Avalonia.Markup.Xaml.MarkupExtensions.DynamicResourceExpression.PublishValue()
at Avalonia.Markup.Xaml.MarkupExtensions.DynamicResourceExpression.StartCore()
at Avalonia.Data.Core.UntypedBindingExpressionBase.Start(Boolean produceValue)
at Avalonia.Data.Core.UntypedBindingExpressionBase.Avalonia.PropertyStore.IValueEntry.HasValue()
at Avalonia.PropertyStore.ValueStore.ReevaluateEffectiveValues(IValueEntry changedValueEntry)
at Avalonia.PropertyStore.ValueStore.EndStyling()
at Avalonia.StyledElement.ApplyStyling()
at Avalonia.StyledElement.EndInit()
at WoWDatabaseEditorCore.Avalonia.Views.MainWindowWithDocking.!XamlIlPopulate(IServiceProvider, MainWindowWithDocking) in /WoWDatabaseEditor/WoWDatabaseEditorCore.Avalonia/Views/MainWindowWithDocking.xaml:line 134
at WoWDatabaseEditorCore.Avalonia.Views.MainWindowWithDocking.!XamlIlPopulateTrampoline(MainWindowWithDocking)
at WoWDatabaseEditorCore.Avalonia.Views.MainWindowWithDocking.InitializeComponent() in /WoWDatabaseEditor/WoWDatabaseEditorCore.Avalonia/Views/MainWindowWithDocking.xaml.cs:line 129
at WoWDatabaseEditorCore.Avalonia.Views.MainWindowWithDocking..ctor(IMainWindowHolder mainWindowHolder, IDocumentManager documentManager, ILayoutViewModelResolver layoutViewModelResolver, IFileSystem fileSystem, IUserSettings userSettings, TempToolbarButtonStyleService tempToolbarButtonStyleService) in /WoWDatabaseEditor/WoWDatabaseEditorCore.Avalonia/Views/MainWindowWithDocking.xaml.cs:line 98
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeWithManyArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
--- End of inner exception stack trace ---
at System.Reflection.MethodBaseInvoker.InvokeWithManyArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Unity.Processors.ConstructorProcessor.<>c__DisplayClass16_0.<GetResolverDelegate>b__0(BuilderContext& c)
at Unity.Processors.MemberProcessor`2.<>c__DisplayClass8_0.<GetResolver>b__0(BuilderContext& c)
at Unity.Processors.MemberProcessor`2.<>c__DisplayClass8_0.<GetResolver>b__0(BuilderContext& c)
at Unity.Processors.MemberProcessor`2.<>c__DisplayClass8_0.<GetResolver>b__0(BuilderContext& c)
at Unity.UnityContainer.<>c__DisplayClass113_0.<OptimizingFactory>b__0(BuilderContext& c)
at Unity.Strategies.BuildPlanStrategy.PreBuildUp(BuilderContext& context)
at Unity.UnityContainer.<>c.<.ctor>b__73_1(BuilderContext& context)
--- End of inner exception stack trace ---
at Unity.UnityContainer.<>c.<.ctor>b__73_1(BuilderContext& context)
at Unity.UnityContainer.Unity.IUnityContainer.Resolve(Type type, String name, ResolverOverride[] overrides)
at Prism.Unity.Ioc.UnityContainerExtension.Resolve(Type type) in /WoWDatabaseEditor/Prism.Avalonia/src/Prism.Unity.Avalonia/Ioc/UnityContainerExtension.cs:line 57
at Prism.Ioc.IContainerProviderExtensions.Resolve[T](IContainerProvider provider)
at WoWDatabaseEditorCore.Avalonia.App.OnFrameworkInitializationCompletedAsync() in /WoWDatabaseEditor/WoWDatabaseEditorCore.Avalonia/App.xaml.cs:line 386
at WoWDatabaseEditorCore.Avalonia.App.<OnFrameworkInitializationCompleted>g__Load|24_0() in /WoWDatabaseEditor/WoWDatabaseEditorCore.Avalonia/App.xaml.cs:line 426
I was able to find that removing this resource
<SolidColorBrush Color="Transparent" x:Key="SystemAltMediumLowColor" />
Fixes the crash. Not sure why it crashes in the first place tho.
To Reproduce
Unfortunately, I wasn't able to create a minimal repro:
-
git clone https://github.com/BAndysc/WoWDatabaseEditor -
git submodule update --init --recursive -
git checkout avalonia11-bug-deferred - Run project
LoaderAvalonia- it will crash
Expected behavior
No response
Avalonia version
11.2.999-cibuild0048166-alpha
OS
Windows, macOS
Additional context
No response
@BAndysc Thanks for the report, the root cause is fixed by #15670: the resource shouldn't have been looked up.
However, please note that SystemAltMediumLowColor is expected to be a Color, not a Brush:
https://github.com/AvaloniaUI/Avalonia/blob/a7f7d80019340420d0571fed47655dbd5d7ed9bf/src/Avalonia.Themes.Fluent/Accents/BaseResources.xaml#L225-L226
This is the code that was effectively throwing, as a SolidColorBrush can't be assigned to a Color (the reverse is allowed, there's an implicit converter from Color to Brush).
Ohh, right! I wonder if the exception could be improved to make it more obvious that it is an user error. Thanks for the fix anyway!