Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/Controls/src/Core/BindableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Dispatching;
Expand Down Expand Up @@ -721,20 +720,19 @@ BindablePropertyContext CreateContext(BindableProperty property)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
BindablePropertyContext GetOrCreateContext(BindableProperty property)
{
#if NETSTANDARD
var context = GetContext(property);
if (context is null)
{
// Create the context before adding it to _properties. CreateContext can invoke a
// DefaultValueCreator that re-entrantly sets other properties, which may resize
// _properties and reallocate its backing storage. Holding a ref into the dictionary
// across that call (e.g. via CollectionsMarshal.GetValueRefOrAddDefault) would leave
// the ref pointing at the discarded array, so the created context would never be
// stored. See https://github.com/dotnet/maui/pull/35970.
context = CreateContext(property);
_properties.Add(property.InternalId, context);
}
#else
ref var context = ref CollectionsMarshal.GetValueRefOrAddDefault(_properties, property.InternalId, out var exists);
if (!exists)
{
context = CreateContext(property);
}
#endif

return context;
}

Expand Down
59 changes: 59 additions & 0 deletions src/Controls/tests/Core.UnitTests/BindableObjectUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,65 @@ public void GetValues()
Assert.Equal(5, values[2]);
}

[Fact]
public void GetValuesReturnsSetStateAndValue()
{
var prop = BindableProperty.Create("Foo", typeof(int), typeof(MockBindable), 0);
var prop1 = BindableProperty.Create("Foo1", typeof(int), typeof(MockBindable), 1);
var prop2 = BindableProperty.Create("Foo2", typeof(int), typeof(MockBindable), 2);
var bindable = new MockBindable();

bindable.SetValue(prop, 3);
bindable.SetValue(prop2, 5);

var values = bindable.GetValues<int>(new[] { prop, prop1, prop2 });

Assert.Equal(3, values.Length);
Assert.True(values[0].IsSet);
Assert.Equal(3, values[0].Value);
Assert.False(values[1].IsSet);
Assert.Equal(0, values[1].Value);
Assert.True(values[2].IsSet);
Assert.Equal(5, values[2].Value);
}

[Fact]
public void DefaultValueCreatorCachesValueWhenReentrantPropertyAddsResizeStore()
{
var reentrantProperties = new BindableProperty[8];
for (var i = 0; i < reentrantProperties.Length; i++)
{
reentrantProperties[i] = BindableProperty.Create($"Reentrant{i}", typeof(int), typeof(MockBindable), 0);
}

var defaultValueCreatorInvocations = 0;
var propertyWithCreator = BindableProperty.Create(
"ReentrantDefault",
typeof(int),
typeof(MockBindable),
0,
defaultValueCreator: b =>
{
defaultValueCreatorInvocations++;
for (var i = 0; i < reentrantProperties.Length; i++)
{
b.SetValue(reentrantProperties[i], i + 1);
}

return 42;
});

var bindable = new MockBindable();

var first = (int)bindable.GetValue(propertyWithCreator);
var second = (int)bindable.GetValue(propertyWithCreator);

Assert.Equal(42, first);
Assert.Equal(42, second);
Assert.Equal(1, defaultValueCreatorInvocations);
Assert.True(bindable.IsSet(propertyWithCreator));
}

class BindingContextConverter
: IValueConverter
{
Expand Down
Loading