c# - Caliburn.Micro attach event handler on event of Behavior -
i wrote own behavior handle swipe gesture , put itemtemplate of listview. if swipe completed, raise event leftswipe or rightswipe. event should handled viewmodel.
i use syntax of caliburn.micro attach handler event: cm:message.attach="[event leftswipe] = [leftswipe($source, $eventargs)"
.
this behavior:
public class swipeinteractionbehavior : dependencyobject, ibehavior { public dependencyobject associatedobject { get; private set; } public void attach(dependencyobject associatedobject) { // ... } public void detach() { // ... } public event eventhandler leftswipe; public event eventhandler rightswipe; // ... // ... private void onleftswipe(frameworkelement element) { // ... if (leftswipe != null) { leftswipe(this, eventargs.empty); } } private void onrightswipe(frameworkelement element) { // ... if (rightswipe != null) { rightswipe(this, eventargs.empty); } } }
this how use behavior inside of listviews itemtemplate:
<listview x:name="links" isitemclickenabled="true" selectionmode="none" cm:message.attach="[event itemclick] = [click($source, $eventargs)]"> <listview.itemtemplate> <datatemplate> <grid> <grid> <border x:name="todoitem" loaded="border_loaded" background="white"> <i:interaction.behaviors> <local:swipeinteractionbehavior cm:message.attach="[event leftswipe] = [leftswipe($source, $eventargs)]" /> </i:interaction.behaviors> <grid> <stackpanel> <textblock text="{binding title}" style="{themeresource listviewitemcontenttextblockstyle}" /> <textblock text="{binding url}" style="{themeresource listviewitemsubheadertextblockstyle}" /> <textblock text="{binding tags, converter={staticresource listtostring}}" /> </stackpanel> </grid> </border> </grid> </grid> </datatemplate> </listview.itemtemplate> </listview>
i ran in exception if raise leftswipe event, stacktrace:
system.exception not handled. hresult=-2146233088 message=no target found method leftswipe. source=caliburn.micro.platform stacktrace: @ caliburn.micro.actionmessage.invoke(object eventargs) @ caliburn.micro.triggeraction`1.execute(object sender, object parameter) @ microsoft.xaml.interactivity.interaction.executeactions(object sender, actioncollection actions, object parameter) @ microsoft.xaml.interactions.core.eventtriggerbehavior.onevent(object sender, object eventargs) @ readerapp.swipeinteractionbehavior.<>c__displayclass5.<onleftswipe>b__4() @ readerapp.extensions.frameworkelementextension.<>c__displayclass2.<animate>b__0(object s, object e) innerexception:
viewmodel:
public class mainviewmodel : viewmodelbase { private readonly ieventaggregator eventaggregator; private readonly database database; public bindablecollection<link> links { get; private set; } public mainviewmodel(inavigationservice navigationservice, ieventaggregator eventaggregator, database database) : base(navigationservice) { this.eventaggregator = eventaggregator; this.database = database; links = new bindablecollection<link>(); } public async void leftswipe(object sender, eventargs e) { // ... } public void rightswipe(object sender, eventargs e) { // ... } }
so problem actionmessage
inherits triggeraction<frameworkelement>
means can't attach correctly swipeinteractionbehavior
.
it's complicated fact there's major api differences between wpf / silverlight / windows phone 8 interactivity sdk , winrt interactivity sdk. can see bit of mean in parser comments.
what i'd recommend implementing swipeinteractionbehavior
trigger behaviour eventtriggerbehavior
, used separate base class winrt it's still ibehavior
, difference has property called actions of type actioncollection
. stupidly there no interface base class enforcing caliburn.micro stuck making assumptions.
you'd use interaction.executeactions
trigger actions, in end xaml should like.
<border x:name="swipetarget"> <i:interaction.behaviors> <local:swipeeventbehavior direction="left"> <cm:actionmessage associatedobject="{binding elementname=swipetarget" method="leftswipe" /> </local:swipeeventbehavior> </i:interaction.behaviors> </border>
it's bit more long winded, need work around limitations imposed changes in sdk.
Comments
Post a Comment