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

SetAppThemeColor crashes with NRE #21503

Closed
AlleSchonWeg opened this issue Mar 28, 2024 · 5 comments · Fixed by #21524 or #21578
Closed

SetAppThemeColor crashes with NRE #21503

AlleSchonWeg opened this issue Mar 28, 2024 · 5 comments · Fixed by #21524 or #21578
Assignees
Labels
area-xaml XAML, CSS, Triggers, Behaviors fixed-in-8.0.20 fixed-in-8.0.40 platform/android 🤖 potential-regression This issue described a possible regression on a currently supported version., verification pending t/bug Something isn't working
Milestone

Comments

@AlleSchonWeg
Copy link

Description

Hi,
i updated to a new Nightly and now my app crashes.
This worked in Version="8.0.20-nightly.10298" but crashes in Version="8.0.20-nightly.10394"

Steps to Reproduce

I can repro the crash with this minimal demo:

	public class Test : BindableObject {
		public static readonly BindableProperty IconColorProperty = BindableProperty.Create(
								propertyName: nameof(IconColor),
								returnType: typeof(Color),
								declaringType: typeof(Test),
								defaultValue: null,
								defaultBindingMode: BindingMode.TwoWay);

		public Color IconColor {
			get => (Color)GetValue(IconColorProperty);
			set => SetValue(IconColorProperty, value);
		}
	}

And then do this:

				var test = new Test();
				test.SetAppThemeColor(Test.IconColorProperty, Colors.Red, Colors.Blue);

I receive a crash:

at Microsoft.Maui.Controls.AppThemeBinding.AppThemeProxy..ctor(Element parent, AppThemeBinding binding) in D:\a_work\1\s\src\Controls\src\Core\AppThemeBinding.cs:line 19
at Microsoft.Maui.Controls.AppThemeBinding.Apply(Object context, BindableObject bindObj, BindableProperty targetProperty, Boolean fromBindingContextChanged, SetterSpecificity specificity) in D:\a_work\1\s\src\Controls\src\Core\AppThemeBinding.cs:line 74
at Microsoft.Maui.Controls.BindableObject.SetBinding(BindableProperty targetProperty, BindingBase binding, SetterSpecificity specificity) in D:\a_work\1\s\src\Controls\src\Core\BindableObject.cs:line 332
at Microsoft.Maui.Controls.BindableObject.SetBinding(BindableProperty targetProperty, BindingBase binding) in D:\a_work\1\s\src\Controls\src\Core\BindableObject.cs:line 288
at Microsoft.Maui.Controls.BindableObjectExtensions.SetAppTheme[Color](BindableObject self, BindableProperty targetProperty, Color light, Color dark) in D:\a_work\1\s\src\Controls\src\Core\BindableObjectExtensions.cs:line 125
at Microsoft.Maui.Controls.BindableObjectExtensions.SetAppThemeColor(BindableObject self, BindableProperty targetProperty,

Link to public reproduction project repository

No response

Version with bug

Nightly / CI build (Please specify exact version)

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

@AlleSchonWeg AlleSchonWeg added the t/bug Something isn't working label Mar 28, 2024
@samhouts samhouts added platform/android 🤖 potential-regression This issue described a possible regression on a currently supported version., verification pending area-xaml XAML, CSS, Triggers, Behaviors labels Mar 28, 2024
@StephaneDelcroix
Copy link
Contributor

the problem is that Test is a BindableObject and not an Element. I can prevent the crash to happen, but you won't get the AppTheme working on it as it's not parented

@AlleSchonWeg
Copy link
Author

AlleSchonWeg commented Mar 29, 2024

Hi @StephaneDelcroix
the Test class items are the items in a collection. I create this collection in a viewmodel (implement INotifyPropertyChanged) and after creation i invoke the PropertyChanged event. Like this:

internal async Task CreateAsync() {
		_ items = new ObservableCollection<Test>()
		var test = new Test();
		test.SetAppThemeColor(Test.IconColorProperty, Colors.Red, Colors.Blue);
		_items.Add(test);
		//Add more to collection 
		PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(Items)));
}
public ObservableCollection<Test> Items {
		get { return _items; }
}

This collection (Items property) is the ItemsSource from a ListView. My goal is if a user changed the theme (dark/light) and the app is running that the color is changing at runtime.
This works well in Xamarin Forms and if i remember correctly it also works in earlier nightly builds.

Edit:
I downgraded the nightly and i can confirm that changing between dark and light mode (via system settings) my icons in the list are changing the colour.

@PureWeen PureWeen added this to the .NET 8 SR4 milestone Mar 29, 2024
@StephaneDelcroix
Copy link
Contributor

Well, that no longer works. Theme propagation is now done through Parenting.

You can still subscribe to the AppThemeChanged event yourself, or parent your elements

@AlleSchonWeg
Copy link
Author

Hi @StephaneDelcroix

with the nightly 10423 the NRE is gone. But now another exeption happens when i change the theme vie system settings:

System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.'

StackTrace:

0xFFFFFFFFFFFFFFFF in Android.Runtime.RuntimeNativeMethods.monodroid_debugger_unhandled_exception C# Annotated Frame
0x1A in Android.Runtime.JNINativeWrapper.unhandled_exception at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:13,5 C# Annotated Frame
0x1E in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:126,26 C# Annotated Frame
0xA in System.ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion C# Annotated Frame
0x13 in System.Collections.Generic.List<System.Action<object,Microsoft.Maui.Controls.Internals.ResourcesChangedEventArgs>>.Enumerator.MoveNextRare C# Annotated Frame
0x4B in System.Collections.Generic.List<System.Action<object,Microsoft.Maui.Controls.Internals.ResourcesChangedEventArgs>>.Enumerator.MoveNext C# Annotated Frame
0x2F in Microsoft.Maui.Controls.Element.OnResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:648,64 C# Annotated Frame
0x2 in Microsoft.Maui.Controls.Element.OnParentResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:623,4 C# Annotated Frame
0x1B in Microsoft.Maui.Controls.VisualElement.OnParentResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:1446,5 C# Annotated Frame
0x16 in Microsoft.Maui.Controls.Element.OnParentResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:618,5 C# Annotated Frame
0x28 in Microsoft.Maui.Controls.Element.OnResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:649,6 C# Annotated Frame
0x2 in Microsoft.Maui.Controls.Element.OnParentResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:623,4 C# Annotated Frame
0x1B in Microsoft.Maui.Controls.VisualElement.OnParentResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:1446,5 C# Annotated Frame
0x16 in Microsoft.Maui.Controls.Element.OnParentResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:618,5 C# Annotated Frame
0x28 in Microsoft.Maui.Controls.Element.OnResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:649,6 C# Annotated Frame
0x10D in Microsoft.Maui.Controls.VisualElement.OnParentResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:1466,5 C# Annotated Frame
0x16 in Microsoft.Maui.Controls.Element.OnParentResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:618,5 C# Annotated Frame
0x28 in Microsoft.Maui.Controls.Element.OnResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:649,6 C# Annotated Frame
0x2 in Microsoft.Maui.Controls.Element.OnParentResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:623,4 C# Annotated Frame
0x1B in Microsoft.Maui.Controls.VisualElement.OnParentResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:1446,5 C# Annotated Frame
0x16 in Microsoft.Maui.Controls.Element.OnParentResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:618,5 C# Annotated Frame
0x28 in Microsoft.Maui.Controls.Element.OnResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:649,6 C# Annotated Frame
0x2 in Microsoft.Maui.Controls.Element.OnParentResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:623,4 C# Annotated Frame
0x1B in Microsoft.Maui.Controls.VisualElement.OnParentResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:1446,5 C# Annotated Frame
0x16 in Microsoft.Maui.Controls.Element.OnParentResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:618,5 C# Annotated Frame
0x28 in Microsoft.Maui.Controls.Element.OnResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:649,6 C# Annotated Frame
0x2 in Microsoft.Maui.Controls.Element.OnParentResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:623,4 C# Annotated Frame
0x16 in Microsoft.Maui.Controls.Element.OnParentResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:618,5 C# Annotated Frame
0x28 in Microsoft.Maui.Controls.Element.OnResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:649,6 C# Annotated Frame
0x2 in Microsoft.Maui.Controls.Element.OnParentResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:623,4 C# Annotated Frame
0x17 in Microsoft.Maui.Controls.Application.OnParentResourcesChanged at D:\a_work\1\s\src\Controls\src\Core\Application\Application.cs:336,5 C# Annotated Frame
0x4B in Microsoft.Maui.Controls.Application.TriggerThemeChangedActual at D:\a_work\1\s\src\Controls\src\Core\Application\Application.cs:255,5 C# Annotated Frame
0x12 in Microsoft.Maui.Controls.Application.set_PlatformAppTheme at D:\a_work\1\s\src\Controls\src\Core\Application\Application.cs:190,5 C# Annotated Frame
0x6 in Microsoft.Maui.Controls.Application.Microsoft.Maui.IApplication.ThemeChanged at D:\a_work\1\s\src\Controls\src\Core\Application\Application.cs:503,4 C# Annotated Frame
0x21 in Microsoft.Maui.LifecycleEvents.AppHostBuilderExtensions.<>c.b__3_0 at D:\a_work\1\s\src\Core\src\Hosting\LifecycleEvents\AppHostBuilderExtensions.Android.cs:77,7 C# Annotated Frame
0xD in Microsoft.Maui.MauiAppCompatActivity. at D:\a_work\1\s\src\Core\src\Platform\Android\MauiAppCompatActivity.Lifecycle.cs:47,114 C# Annotated Frame
0x1F in Microsoft.Maui.LifecycleEvents.LifecycleEventServiceExtensions.InvokeLifecycleEvents<Microsoft.Maui.LifecycleEvents.AndroidLifecycle.OnConfigurationChanged> at D:\a_work\1\s\src\Core\src\LifecycleEvents\LifecycleEventServiceExtensions.cs:31,5 C# Annotated Frame
0x40 in Microsoft.Maui.MauiAppCompatActivity.OnConfigurationChanged at D:\a_work\1\s\src\Core\src\Platform\Android\MauiAppCompatActivity.Lifecycle.cs:47,4 C# Annotated Frame
0x11 in Android.App.Activity.n_OnConfigurationChanged_Landroid_content_res_Configuration
at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Android.App.Activity.cs:2926,4 C# Annotated Frame
0x9 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:125,5 C# Annotated Frame

Which collection was modified? Downgrading nightly and my app works again.

@AlleSchonWeg
Copy link
Author

Well, that no longer works. Theme propagation is now done through Parenting.

Hi @StephaneDelcroix
what ist Parenting? Is there a documentation how this works?
Thank you

@github-actions github-actions bot locked and limited conversation to collaborators May 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-xaml XAML, CSS, Triggers, Behaviors fixed-in-8.0.20 fixed-in-8.0.40 platform/android 🤖 potential-regression This issue described a possible regression on a currently supported version., verification pending t/bug Something isn't working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants