Caliburn.Micro在Behavior事件上附加事件处理程序

时间:2015-06-13 18:33:33

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

1 个答案:

答案 0 :(得分:2)

问题是ActionMessage继承了TriggerAction<FrameworkElement>,这意味着它无法正确附加到SwipeInteractionBehavior

WPF / Silverlight / Windows Phone 8 Interactivity SDK和WinRT Interactivity SDK之间存在一些主要的API差异,这也很复杂。您可以在Parser评论中看到我的意思。

我建议将SwipeInteractionBehavior实现为触发器行为,就像EventTriggerBehavior一样,这曾经是一个单独的基类,但是使用WinRT它仍然是{{1}不同之处在于它有一个名为Actions IBehavior的属性。愚蠢的是没有接口是基类强制执行这一点所以Caliburn.Micro陷入了做出一些假设。

然后您使用ActionCollection来触发这些操作,最后您的xaml应该看起来像。

Interaction.ExecuteActions

这有点长篇大论,但我们需要解决SDK中的变化带来的限制。

相关问题