Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Behavior issue

$
0
0

Good aftnernoon ladies and gents.

My situation is as followed:

  • Portable project with a Custom Behavior in it.
  • Regular Xamarin project which has a reference to the Portable project in the Shared code project.

The Behavior in question (it's based on a Custom Entry which i named Entry :) )

using System;
using System.Windows.Input;
using XamarinForms = Xamarin.Forms;

namespace ChipSoft.Mobile.Core.Presentation.Xamarin.Behaviors
{
    public class CompletedEventBehavior : XamarinForms.Behavior<Entry>
    {
        public static readonly XamarinForms.BindableProperty CommandProperty = XamarinForms.BindableProperty.Create<CompletedEventBehavior, ICommand>
        (
            p => p.Command,
            null,
            propertyChanged: OnCommandChanged
        );

        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        public static void OnCommandChanged(XamarinForms.BindableObject bo, ICommand oldValue, ICommand newValue)
        {
            bo.SetValue(CommandProperty, newValue);
        }

        protected override void OnAttachedTo(Entry entry)
        {
            entry.Completed += OnEntryCompleted;
            base.OnAttachedTo(entry);
        }

        protected override void OnDetachingFrom(Entry entry)
        {
            entry.Completed -= OnEntryCompleted;
            base.OnDetachingFrom(entry);
        }

        void OnEntryCompleted(object sender, EventArgs args)
        {
            if (Command != null)
            {
                if (Command.CanExecute(null))
                {
                    Command.Execute(null);
                }
            }
        }
    }
}

Xaml usage:

<Entry.Behaviors>
   <behaviors:CompletedEventBehavior Command="{Binding LoginCommand}"/>
</Entry.Behaviors>

The LoginCommand does what it says it does :)

The error I am getting complete with Stacktrace:

A first chance exception of type 'System.InvalidOperationException' occurred in Xamarin.Forms.Core.DLL

Additional information: bindable not an instance of AssociatedType

at Xamarin.Forms.Behavior.Xamarin.Forms.IAttachedObject.AttachTo(BindableObject bindable)

The question is quite simple, why doesn't this work?
It is based on several posts online and it worked before we moved the Behavior itself to the portable project.
We are using the latest Xamarin.Core and Xamarin.Forms versions.


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>