microsoft / XamlBehaviors

This is the official home for UWP XAML Behaviors on GitHub.
MIT License
697 stars 112 forks source link

UWP - Compatibility with Xamarin.Forms.Behavior - No suitable method found to override (OnAttachedTo etc) #165

Closed rhy-ama closed 4 years ago

rhy-ama commented 4 years ago

Hello,

I've installed the Microsoft.Xaml.Behaviors.Uwp.Managed nuget 2.0.1 and referenced Behavior<T> with using Microsoft.Xaml.Interactivity;.

However, on the example of a simple masking behavior below I get

...error CS0115: 'MaskedBehavior.OnAttachedTo(TextBox)': no suitable method found to override
...error CS0115: 'MaskedBehavior.OnDetachingFrom(TextBox)': no suitable method found to override

What am I missing? Is the nugget not compatible with Xamarin (inc. UWP) behaviors Xamarin.Forms.Behavior ?

This is what I see in the object browser:

namespace Microsoft.Xaml.Interactivity
{
    //
    // Summary:
    //     A base class for behaviors making them code compatible with older frameworks,
    //     and allow for typed associtated objects.
    //
    // Type parameters:
    //   T:
    //     The object type to attach to
    public abstract class Behavior<T> : Behavior where T : DependencyObject
    {
        protected Behavior();

        //
        // Summary:
        //     Gets the object to which this behavior is attached.
        public T AssociatedObject { get; }

        //
        // Summary:
        //     Called after the behavior is attached to the Microsoft.Xaml.Interactivity.Behavior.AssociatedObject.
        //
        // Remarks:
        //     Override this to hook up functionality to the Microsoft.Xaml.Interactivity.Behavior.AssociatedObject
        protected override void OnAttached();
    }
}

Sample I am trying to get to work:

    public class MaskedBehavior : Behavior<TextBox>
    {
        private string _mask = "";
        public string Mask
        {
            get => _mask;
            set
            {
                _mask = value;
                SetPositions();
            }
        }

        protected override void OnAttachedTo(TextBox entry)
        {
            entry.TextChanged += OnEntryTextChanged;
            base.OnAttachedTo(entry);
        }

        protected override void OnDetachingFrom(TextBox entry)
        {
            entry.TextChanged -= OnEntryTextChanged;
            base.OnDetachingFrom(entry);
        }

        IDictionary<int, char> _positions;

        void SetPositions()
        {
            if (string.IsNullOrEmpty(Mask))
            {
                _positions = null;
                return;
            }

            var list = new Dictionary<int, char>();
            for (var i = 0; i < Mask.Length; i++)
                if (Mask[i] != 'X')
                    list.Add(i, Mask[i]);

            _positions = list;
        }

        private void OnEntryTextChanged(object sender, TextChangedEventArgs args)
        {
            var entry = sender as TextBox;

            var text = entry.Text;

            if (string.IsNullOrWhiteSpace(text) || _positions == null)
                return;

            if (text.Length > _mask.Length)
            {
                entry.Text = text.Remove(text.Length - 1);
                return;
            }

            foreach (var position in _positions)
                if (text.Length >= position.Key + 1)
                {
                    var value = position.Value.ToString();
                    if (text.Substring(position.Key, 1) != value)
                        text = text.Insert(position.Key, value);
                }

            if (entry.Text != text)
                entry.Text = text;
        }
    }

Target and min version are 1903 (build 18362) and project has Microsoft.NETCore.UniversalWindowsPlatform v6.2.9 in it. VS 2019 v 16.4.0

pedrolamas commented 4 years ago

What am I missing? Is the nugget not compatible with Xamarin (inc. UWP) behaviors Xamarin.Forms.Behavior ?

No, Xamarin uses their own behaviors, so they are not compatible.