Behavior issue

GattzGattz USMember
edited October 2016 in Xamarin.Forms

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.

Answers

  • FredgieFredgie USMember ✭✭

    i would comment out the line:

    bo.SetValue(CommandProperty, newValue);

    its redundant, and might be causing problems

  • GattzGattz USMember
    edited December 2015

    I have gotten around to playing with the Behavior once more. (sorry for the delay)
    As suggested I got rid of the OnPropChanged which in its method had the bo.SetValue.

    Currently when I run the app with the Behavior active I end up with a System.InvalidOperationException with the following message:
    bindable not an instance of AssociatedType

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

    For extra clarity this exception is shown in the generated code file for the View which has the Behavior in it.

    Any suggestions as to what I can do to fix this exception?

    P.S.:
    Made sure to new use the base Xamarin.Forms.Entry.
    To make sure there would be no confusion caused by the custom props added on the Custom entry, nor the Renderers.

  • JohnHardmanJohnHardman GBUniversity mod

    @Gattz - Did you ever work out what was causing this? I've been re-factoring a pile of code today, and am suddenly getting this error.

  • JohnHardmanJohnHardman GBUniversity mod

    @Gattz - Just spotted the cause. No worries.

  • rvanderkooyrvanderkooy USMember
    edited October 2016

    @JohnHardman @Gattz Do you have any suggestions as to how to resolve this? I'm trying to get my ContentPage to call the LoadData command in my ViewModel (so i don't have to do it in the codebehind).

    <ContentPage.Behaviors> <myBehaviors:EventToCommandBehavior EventName="Appearing" Command="{Binding LoadData}"/> </ContentPage.Behaviors>

    I'm getting the same "bindable not an instance of AssociatedType" error.

    I'm using the EventToCommandBehavior that is defined here...
    https://github.com/xamarin/xamarin-forms-samples/tree/master/Behaviors/EventToCommandBehavior/EventToCommandBehavior/Behaviors

    Thanks, -Ryan

  • ShimmyWeitzhandlerShimmyWeitzhandler USMember ✭✭✭

    @rvanderkooy said:
    Ah, i figured it out.
    Seems to work....

    Thanks so much for sharing!

  • DavidPuplavaDavidPuplava USMember ✭✭

    Thanks, @rvanderkooy. I used the same EventToCommandBehavior class trying to bind a view model command to a ContentPage's Disappearing event which triggered the same error. Changing the generic parameter to be VisualElement instead of View definitely fixes it. It makes sense given that ContentPage descends from VisualElement rather than View.

    Thanks for your post.

  • stijnvstijnv BEMember

    Thumps up @rvanderkooy

  • JohnPrassJohnPrass Member ✭✭

    Thank you @rvanderkooy.

Sign In or Register to comment.