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

Avoid throwing in async void when applying relative source bindings #24780

Merged
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
18 changes: 12 additions & 6 deletions src/Controls/src/Core/Binding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,17 @@ internal override void Apply(object context, BindableObject bindObj, BindablePro

if (Source is RelativeBindingSource relativeBindingSource)
{
ApplyRelativeSourceBinding(relativeBindingSource, bindObj, targetProperty, specificity);
var relativeSourceTarget = RelativeSourceTargetOverride ?? bindObj as Element;
if (relativeSourceTarget is not Element)
{
var message = bindObj is not null
? $"Cannot apply relative binding to {bindObj.GetType().FullName} because it is not a superclass of Element."
: "Cannot apply relative binding when the target object is null.";

throw new InvalidOperationException(message);
}

ApplyRelativeSourceBinding(relativeBindingSource, relativeSourceTarget, bindObj, targetProperty, specificity);
}
else
{
Expand All @@ -148,13 +158,9 @@ internal override void Apply(object context, BindableObject bindObj, BindablePro
}

#pragma warning disable RECS0165 // Asynchronous methods should return a Task instead of void
async void ApplyRelativeSourceBinding(RelativeBindingSource relativeSource, BindableObject targetObject, BindableProperty targetProperty, SetterSpecificity specificity)
async void ApplyRelativeSourceBinding(RelativeBindingSource relativeSource, Element relativeSourceTarget, BindableObject targetObject, BindableProperty targetProperty, SetterSpecificity specificity)
#pragma warning restore RECS0165 // Asynchronous methods should return a Task instead of void
{
var relativeSourceTarget = RelativeSourceTargetOverride ?? targetObject as Element;
if (!(relativeSourceTarget is Element))
throw new InvalidOperationException();

await relativeSource.Apply(_expression, relativeSourceTarget, targetObject, targetProperty, specificity);
}

Expand Down
18 changes: 12 additions & 6 deletions src/Controls/src/Core/TypedBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,17 @@ internal override void Apply(object context, BindableObject bindObj, BindablePro

if (Source is RelativeBindingSource relativeSource)
{
ApplyRelativeSourceBinding(relativeSource, bindObj, targetProperty, specificity);
var relativeSourceTarget = RelativeSourceTargetOverride ?? bindObj as Element;
if (relativeSourceTarget is not Element)
{
var message = bindObj is not null
? $"Cannot apply relative binding to {bindObj.GetType().FullName} because it is not a superclass of Element."
: "Cannot apply relative binding when the target object is null.";

throw new InvalidOperationException(message);
}

ApplyRelativeSourceBinding(relativeSource, relativeSourceTarget, bindObj, targetProperty, specificity);
}
else
{
Expand All @@ -192,13 +202,9 @@ internal override void Apply(object context, BindableObject bindObj, BindablePro

#pragma warning disable RECS0165 // Asynchronous methods should return a Task instead of void
async void ApplyRelativeSourceBinding(
RelativeBindingSource relativeSource, BindableObject targetObject, BindableProperty targetProperty, SetterSpecificity specificity)
RelativeBindingSource relativeSource, Element relativeSourceTarget, BindableObject targetObject, BindableProperty targetProperty, SetterSpecificity specificity)
#pragma warning restore RECS0165 // Asynchronous methods should return a Task instead of void
{
var relativeSourceTarget = RelativeSourceTargetOverride ?? targetObject as Element;
if (!(relativeSourceTarget is Element))
throw new InvalidOperationException();

await relativeSource.Apply(this, relativeSourceTarget, targetObject, targetProperty, specificity);
}

Expand Down
Loading