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

[BUG] Set nullable BindableProperty to null throws Exception #354

Open
2 tasks done
JoeVoo opened this issue Dec 21, 2024 · 0 comments
Open
2 tasks done

[BUG] Set nullable BindableProperty to null throws Exception #354

JoeVoo opened this issue Dec 21, 2024 · 0 comments
Labels
bug Something isn't working unverified Bug has not been verified by maintainers

Comments

@JoeVoo
Copy link

JoeVoo commented Dec 21, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

Setting a bindable and nullable property to null throws an exception "Unable to find target value".

Expected Behavior

should be ok
this worked in "CommunityToolkit.Maui.Markup" Version="4.2.0"

Steps To Reproduce

In my case, the problem occurs in a special control.
The control is defined in nullable environment with generic struct to let a enum picker be set to null.

public partial class EnumPicker : Picker where T : struct, Enum //allow null with struct
...

the property is defined:

public static readonly BindableProperty SelectedValueProperty =
    BindableProperty.Create(
    nameof(SelectedValue),
    typeof(T?),
    typeof(EnumPicker<T>),
    null,
    BindingMode.TwoWay,
    null,
    propertyChanged: SelectedValue_Changed);

  public T? SelectedValue
  {
      get => (T?)GetValue(SelectedValueProperty);
      **set => SetValue(SelectedValueProperty, value);**
  }

The control is used at a contenview:

     enumPicker = new EnumPicker<T>()
     {
         Margin = new Thickness(0, 5, 0, 0),
         VerticalTextAlignment = TextAlignment.Center
     }
     .Bind(EnumPicker<T>.SelectedValueProperty,
         getter: static (EnumPickerControl<T> control) => control.SelectedValue,
         setter: static (EnumPickerControl<T> control, T? value) => control.SelectedValue = value,
         source: this);

After giving the Selected Value an enum value and set it back to null, the exception will be thrown at
set => SetValue(SelectedValueProperty, value);

Link to public reproduction project repository

hope this is not needed

Environment

- .NET MAUI C# Markup CommunityToolkit: 5.1.0
- CommunityToolkit.Maui: 10.0.0
- OS: Windows 10 Build 10.0.19041.0
- .NET MAUI: 9.0.21

Anything else?

Remove the exception throw in your TypedBinding.cs, Line 249:

var value = GetTargetValue(target.GetValue(property), typeof(TProperty))/* ?? throw new InvalidOperationException("Unable to find target value")*/;

returning a null is not a fault here. For my use, after removing the exception, it works than as it should.

More testing makes no sense for me, because i don't know the intention of throwing this exception here.

@JoeVoo JoeVoo added bug Something isn't working unverified Bug has not been verified by maintainers labels Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working unverified Bug has not been verified by maintainers
Projects
None yet
Development

No branches or pull requests

1 participant