Good aftnernoon ladies and gents.
My situation is as followed:
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
i would comment out the line:
bo.SetValue(CommandProperty, newValue);
its redundant, and might be causing problems
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.
@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.
@Gattz - Just spotted the cause. No worries.
@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
Ah, i figured it out. I changed the EventToCommandBehavior to inherit from
BehaviorBase<VisualElement>
instead of
BehaviorBase<View>
Seems to work....
Thanks so much for sharing!
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 ofView
definitely fixes it. It makes sense given thatContentPage
descends fromVisualElement
rather thanView
.Thanks for your post.
Thumps up @rvanderkooy
Thank you @rvanderkooy.