Caliburn.Micro 将事件处理程序附加到行为事件上

本文关键字:事件 Micro 事件处理 程序 Caliburn | 更新日期: 2023-09-27 17:55:52

我编写了自己的行为来处理滑动手势并将其放入列表视图的 ItemTemplate。如果滑动完成,我会引发一个事件,用于向左滑动或向右滑动。此事件应由我的视图模型处理。

我使用 Caliburn.Micro 的语法将处理程序附加到事件: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: 

视图模型:

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)
    {
        // ...
    }
}

Caliburn.Micro 将事件处理程序附加到行为事件上

所以问题是ActionMessage继承了TriggerAction<FrameworkElement>这意味着它不能正确附加到SwipeInteractionBehavior

WPF/

Silverlight/Windows Phone 8 Interactive SDK和WinRT Interactive SDK之间存在一些主要的API差异,因此情况也很复杂。你可以在解析器注释中看到我的意思。

我建议的是像EventTriggerBehavior一样将SwipeInteractionBehavior实现为触发器行为,这曾经是一个单独的基类,但对于 WinRT,它仍然是IBehavior的,不同之处在于它具有一个名为 ActionCollection 类型的操作的属性。愚蠢的是,没有接口是基类强制执行这一点,因此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 更改带来的限制。