Support Nullable Bindable Properties in TypedBinding.cs#371
Support Nullable Bindable Properties in TypedBinding.cs#371JoeVoo wants to merge 3 commits intoCommunityToolkit:mainfrom
TypedBinding.cs#371Conversation
Remove the null value Exception
| if (needsSetter && sourceObject is not null) | ||
| { | ||
| var value = GetTargetValue(target.GetValue(property), typeof(TProperty)) ?? throw new InvalidOperationException("Unable to find target value"); | ||
| var value = GetTargetValue(target.GetValue(property), typeof(TProperty)); |
There was a problem hiding this comment.
I wonder if we should check whether TProperty is nullable in this scenario. Thoughts @TheCodeTraveler?
There was a problem hiding this comment.
Thanks @JoeVoo! Could you please add unit tests that demonstrate the bug you are fixing and that verify the fix in this PR? It'd be great if you could also update the sample app to include a demonstration of how users were impacted; we use the sample app as a regression test against new PRs to ensure they don't break any existing behavior.
I've re-added the PR Checklist from our Pull Request Template to this PR Description that contains this checklist.
|
@dotnet-policy-service agree |
|
Nullable reference types are enabled in the Unit Tests. I think this happens only in a 'nullable ref type disabled' project. But i am no test specialist. I try to avoid using enable. public partial class EnumPicker<T> : Picker where T : struct, Enum
...
public static readonly BindableProperty SelectedValueProperty =
BindableProperty.Create(
nameof(SelectedValue),
typeof(T?),
typeof(EnumPicker<T>),
null,
BindingMode.TwoWay,
null,
propertyChanged: SelectedValue_Changed);typeof(T?) and EnumOfT is not nice, but the only way i found to set it null |
|
@bijington , what is your scenario to get the exception? |
|
I am not experiencing this issue but I would be happy to try and contribute a unit test which would highlight the issue and fix if that helps? |
TypedBinding.cs
newerly i got an error message on the TryConvert line with ref value is null. I managed this with the added lines.
|
Closing in favor of #381; same solution with unit tests included |
Remove the null value Exception
Description of Change
Remove the exception fired on null value that is not needed
var value = GetTargetValue(target.GetValue(property), typeof(TProperty)) ?? throw new InvalidOperationException("Unable to find target value");Linked Issues
PR Checklist
approved(bug) orChampioned(feature/proposal)mainat time of PRAdditional information