Skip to content

Calling SetBinding after setting a constant value crashes #17776

@fsdsabel

Description

@fsdsabel

Description

If I set a binding on a control in code behind after a constant value has been set, the application crashes with an ArgumentException: An item with the same key has already been added. Key: Microsoft.Maui.Controls.SetterSpecificity (Parameter 'key')

This is new behavior in .NET 8 as I cannot reproduce this with .NET 7. We use this heavily in our quite large application with many user controls and for us it is almost a blocker for migration to .NET 8. There might be a workaround but it's difficult to spot all the instances this is happening. We are really looking forward to 8 as we are dealing with a lot of bugs in 7 that are really slowing us down.

Steps to Reproduce

  1. Create a File -> New MAUI App
  2. Replace the content of OnCounterClicked in MainPage.xaml.cs with this:
// if CounterBtn already has a binding set on BackgroundColor, this sequence crashes immediately, otherwise it crashes the second time it's called
CounterBtn.BackgroundColor = Colors.Coral;            
CounterBtn.SetBinding(BackgroundColorProperty, new Binding(nameof(BackgroundColor), source: this));
  1. Run the app (verified on Windows)
  2. Click the "Click me" button twice. The second time the app will crash.

Link to public reproduction project repository

No response

Version with bug

8.0.0-rc.1.9171

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

7.0.92

Affected platforms

Android, Windows, I was not able test on other platforms

Affected platform versions

All

Did you find any workaround?

No. Even clearing the binding or setting the property to null doesn't help.

Relevant log output

No response

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions