C# 行为事件上的微附加事件处理程序
我编写了自己的行为来处理滑动手势,并将其放入ListView的ItemTemplate中。如果刷卡完成,我会为LeftSwipe或RightSwipe引发一个事件。此事件应由我的ViewModel处理 我使用Caliburn.Micro的语法将处理程序附加到事件:C# 行为事件上的微附加事件处理程序,c#,xaml,winrt-xaml,caliburn.micro,behavior,C#,Xaml,Winrt Xaml,Caliburn.micro,Behavior,我编写了自己的行为来处理滑动手势,并将其放入ListView的ItemTemplate中。如果刷卡完成,我会为LeftSwipe或RightSwipe引发一个事件。此事件应由我的ViewModel处理 我使用Caliburn.Micro的语法将处理程序附加到事件:cm:Message.attach=“[event LeftSwipe]=[LeftSwipe($source,$eventArgs)” 这是我的行为: public class SwipeInteractionBehavior : D
cm:Message.attach=“[event LeftSwipe]=[LeftSwipe($source,$eventArgs)”
这是我的行为:
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);
}
}
}
以下是我在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>
如果引发LeftSwipe事件,我在异常情况下运行,这是我的StackTrace:
System.Exception was not handled.
HResult=-2146233088
Message=No target found for method LeftSwipe.
Source=Caliburn.Micro.Platform
StackTrace:
at Caliburn.Micro.ActionMessage.Invoke(Object eventArgs)
at Caliburn.Micro.TriggerAction`1.Execute(Object sender, Object parameter)
at Microsoft.Xaml.Interactivity.Interaction.ExecuteActions(Object sender, ActionCollection actions, Object parameter)
at Microsoft.Xaml.Interactions.Core.EventTriggerBehavior.OnEvent(Object sender, Object eventArgs)
at ReaderApp.SwipeInteractionBehavior.<>c__DisplayClass5.<OnLeftSwipe>b__4()
at ReaderApp.Extensions.FrameworkElementExtension.<>c__DisplayClass2.<Animate>b__0(Object s, Object e)
InnerException:
未处理System.Exception。
HResult=-2146233088
Message=未找到方法LeftSwipe的目标。
Source=Caliburn.Micro.Platform
堆栈跟踪:
在Caliburn.Micro.ActionMessage.Invoke(对象事件参数)
在Caliburn.Micro.TriggerAction`1.Execute处(对象发送器,对象参数)
在Microsoft.Xaml.Interactivity.Interaction.ExecuteActions(对象发送器、ActionCollection操作、对象参数)
位于Microsoft.Xaml.Interactions.Core.EventTriggerBehavior.OneEvent(对象发送方,对象事件参数)
在ReaderApp.SwipeInteractionBehavior.c__DisplayClass5.b__4()中
在ReaderApp.Extensions.FrameworkElementExtension.c__DisplayClass2.b__0(对象s、对象e)
内部异常:
视图模型:
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)
{
// ...
}
}
public类MainViewModel:ViewModelBase
{
私有只读IEventagor事件聚合器;
专用只读数据库;
公共BindableCollection链接
{
得到;
私人设置;
}
public Main视图模型(INavigationService navigationService、IEventAggregator事件聚合器、数据库)
:base(导航服务)
{
this.eventAggregator=eventAggregator;
this.database=数据库;
Links=新的BindableCollection();
}
公共异步void LeftSwipe(对象发送方,事件参数e)
{
// ...
}
public void RightSwipe(对象发送方,事件参数e)
{
// ...
}
}
所以问题在于ActionMessage
继承了TriggerAction
,这意味着它无法正确地附加到SwipeInteractionBehavior
WPF/Silverlight/Windows Phone 8交互SDK和WinRT交互SDK之间存在一些主要的API差异,这也让问题变得复杂。您可以在评论中看到我的一些意思
我建议将SwipeInteractionBehavior
实现为触发器行为,就像EventTriggerBehavior
,这曾经是一个单独的基类,但对于WinRT,它仍然是IBehavior
,区别在于它有一个名为Actions的属性,类型为ActionCollection
。愚蠢的是,没有I接口是实现这一点的基类,所以Caliburn.Micro一直在做一些假设
然后使用Interaction.ExecuteActions
来触发这些操作,最终您的xaml应该是这样的
<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>
这有点冗长,但我们需要绕过SDK中的更改所施加的限制。似乎找不到LeftSwipe方法,您可以共享LinkViewModel(或绑定到ListView的任何内容)的视图模型代码吗我添加了ViewModel代码。如果我将SwipeInteraction实现为ContentControl,它将毫无问题地工作。似乎不可能在行为上使用附加属性。我需要研究一下,另一种可能的方法是将SwipeInteraction实现为触发器,使用ActionMessage作为操作。非常感谢您的支持回答!