maui icon indicating copy to clipboard operation
maui copied to clipboard

[.Net 8] Bindable Properties Initialization with default new()

Open Phenek opened this issue 10 months ago • 0 comments

Description

When attempting to define a bindable property with default new() value in a .NET MAUI project, I encountered a crash related to the initialization of the default property.

public static readonly BindableProperty TabItemsProperty = BindableProperty.Create(
nameof(TabItems), typeof(ObservableCollection<Tab>), typeof(TabulationPage), new());

At least you will have a crash with the only information: Exception thrown: 'System.ArgumentException' in Microsoft.Maui.Controls.dll

Depends if your application is align with the moon, you could find a better error. For me I do not have information about the error location... It was really hard to debug.

Steps to Reproduce

1. Define a bindable property with default value new().
2. Use this Bindable property in a Page that is display.
2. Compile the project and observe the result.

Link to public reproduction project repository

No response

Version with bug

7.0.49

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, Windows, macOS

Affected platform versions

.Net 7 to .Net 8

Did you find any workaround?

Don't use new(), define the all default property type.

public static readonly BindableProperty TabItemsProperty = BindableProperty.Create(
nameof(TabItems), typeof(ObservableCollection<Tab>), typeof(TabulationPage), 
new ObservableCollection<Tab>());

Relevant log output

No response

Phenek avatar Apr 15 '24 08:04 Phenek