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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow up to #1378: NullRef exception in Target Binding when using a value type + fix #3343

Closed
1 of 7 tasks
softlion opened this issue Mar 27, 2019 · 0 comments
Closed
1 of 7 tasks
Labels
t/bug Bug type up-for-grabs Issues up for grabs by anyone
Milestone

Comments

@softlion
Copy link
Contributor

馃悰 Bug Report

When creating a target binding to a value type using the generic class, SetValue will fail in the conversion from null to the value type, as the value type can not be null.

Expected behavior

No exception.

Reproduction steps

I tracked the problem to be in class MvxTargetBinding<TTarget, TValue>.

image

The problem is the conversion from null to a value type (here a "bool") which can not be null.

I don't know why the binding would send null in first place.
The caller is C:\projects\mvvmcross\MvvmCross\Binding\Bindings\MvxFullBinding.cs:181,25

This can be reproduced easily by creating a binding for "Selected" on Android, then binding a bool value to the Selected property of any view:

    public class SelectedTargetBinding : MvxAndroidTargetBinding<View, bool>
    {
        public const string Name = "Selected";

        public override MvxBindingMode DefaultMode => MvxBindingMode.OneWay;

        public static void Register(IMvxTargetBindingFactoryRegistry registry)
        {
            registry.RegisterCustomBindingFactory<View>(Name, view => new SelectedTargetBinding(view));
        }

        [Android.Runtime.Preserve(Conditional = true)]
        public SelectedTargetBinding(View target) : base(target)
        {
        }

        protected override void SetValueImpl(View target, bool value)
        {
            if (target != null && value != target.Selected)
                target.Selected = value;
        }
    }

A solution would be to update MvxTargetBinding<TTarget, TValue>:

        public void SetValue(object value)
        {
            SetValue(value == null ? default(TValue) : (TValue) value);
        }

Configuration

Version: 6.2.3

Platform:

  • 馃摫 iOS
  • 馃 Android
  • 馃弫 WPF
  • 馃寧 UWP
  • 馃崕 MacOS
  • 馃摵 tvOS
  • 馃悞 Xamarin.Forms
@Cheesebaron Cheesebaron added t/bug Bug type up-for-grabs Issues up for grabs by anyone p/android Android platform and removed p/android Android platform labels Mar 27, 2019
@Cheesebaron Cheesebaron added this to the 6.2.4 milestone Mar 27, 2019
@softlion softlion changed the title Follow up to #1378: NullRef exception in Target Binding when using a value type Follow up to #1378: NullRef exception in Target Binding when using a value type + fix Apr 3, 2019
@martijn00 martijn00 modified the milestones: 6.2.4, 6.3.0 May 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
t/bug Bug type up-for-grabs Issues up for grabs by anyone
Development

No branches or pull requests

3 participants