スワイプジェスチャを処理して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);
}
}
}
これは、ListViewsItemTemplate内でこの動作を使用する方法です。
<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:
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)
{
// ...
}
}
したがって、問題はActionMessage
継承TriggerAction<FrameworkElement>
することSwipeInteractionBehavior
です。つまり、に正しく接続できません。
また、WPF / Silverlight / Windows Phone8インタラクティブSDKとWinRTインタラクティブSDKの間にいくつかの主要なAPIの違いがあるという事実によって複雑になっています。パーサーのコメントで私が言っていることを少し見ることができます。
私がお勧めするのはSwipeInteractionBehavior
、トリガービヘイビアとして実装することですEventTriggerBehavior
。これは以前は別の基本クラスでしたが、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の変更によって課せられる制限を回避する必要があります。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加