请问silverlight使用mvvm light如何绑定一个Behavior中的事件

juezhao007 2013-07-30 04:25:43
下面是我自己写的behavior 有两个事件LeftFlick和RightFlick

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;

namespace CardsChina.Behavior
{
public class CustomLRFlickAction : Behavior<UIElement>
{
protected bool MouseDown { get; set; }
Point? downPoint;
//左滑事件
public event EventHandler LeftFlick;
//右滑事件
public event EventHandler RightFlick;
public double MinOffsetProperty
{
get { return (double)GetValue(MinOffsetPropertyProperty); }
set { SetValue(MinOffsetPropertyProperty, value); }
}

// Using a DependencyProperty as the backing store for MaxOffsetProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MinOffsetPropertyProperty =
DependencyProperty.Register("MinOffsetProperty", typeof(double), typeof(CustomLRFlickAction), new PropertyMetadata(20.0));

protected override void OnAttached()
{
base.OnAttached();

this.AssociatedObject.MouseLeftButtonDown += AO_MouseLeftButtonDown;
this.AssociatedObject.MouseLeftButtonUp += AO_MouseLeftButtonUp;
}

protected override void OnDetaching()
{
this.AssociatedObject.MouseLeftButtonDown -= AO_MouseLeftButtonDown;
this.AssociatedObject.MouseLeftButtonUp -= AO_MouseLeftButtonUp;

base.OnDetaching();
}
protected virtual void AO_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (!MouseDown)
{
MouseDown = false;
return;
}
var upPoint = e.GetPosition(null);
if (!downPoint.HasValue)
{
MouseDown = false;
return;
}
var offset = new Point(upPoint.X - downPoint.Value.X, upPoint.Y - downPoint.Value.Y);
System.Diagnostics.Debug.WriteLine("之前点---->{0}", downPoint);
System.Diagnostics.Debug.WriteLine("之后点---->{0}", upPoint);
System.Diagnostics.Debug.WriteLine(offset);
if (offset.Y > MinOffsetProperty)
{
OnRightFlick();
MouseDown = false;
return;
}
if (offset.Y <-MinOffsetProperty)
{
OnLeftFlick();
MouseDown = false;
return;
}
MouseDown = false;
}

protected virtual void AO_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MouseDown = true;
downPoint = e.GetPosition(null);
System.Diagnostics.Debug.WriteLine("之前点---->{0}", downPoint);
}

protected virtual void OnLeftFlick()
{
if (LeftFlick != null)
{
LeftFlick(this.AssociatedObject, EventArgs.Empty);
}
}
protected virtual void OnRightFlick()
{
if (RightFlick != null)
{
RightFlick(this.AssociatedObject, EventArgs.Empty);
}
}
}
}
请问大家 现在我要在Xaml中和viewModel中如何才能实现如触发LeftFlick事件绑定相应方法呢?

我现在在xaml中是如下写的 但是没用
<Rectangle Fill="Blue" Width="400" Height="200" Margin="28,397,28,10">
<i:Interaction.Triggers>
<i:EventTrigger EventName="LeftFlick">
<i:InvokeCommandAction Command="{Binding LeftAnimation}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Rectangle>
...全文
116 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
juezhao007 2013-07-30
  • 打赏
  • 举报
回复
自己顶一下吧。。都沉了
内容概要:本文档是一份关于开发经典小游戏“贪吃蛇”的保姆级毕业设计指导文档,涵盖从游戏规则、功能设计、技术选型到核心代码实现的完整流程。文档以Python + Pygame为技术栈,详细讲解了游戏初始化、蛇的移动逻辑、食物生成、碰撞检测、计分与等级系统、键盘控制及主循环渲染等核心模块,并提供了完整的项目结构和源码示例。采用状态机管理游戏状态,使用列表模拟链表结构存储蛇身,结合方向缓冲机制优化操作体验,同时引入难度递增机制提升可玩性。文末还列举了飞机大战、扫雷、五子棋等同类课题作为拓展参考。; 适合人群:具备一定编程基础、正在进行毕业设计或课程设计的学生,尤其是对游戏开发感兴趣的初级开发者。; 使用场景及目标:①帮助学生独立完成一个功能完整、结构清晰的小游戏项目,用于毕业答辩或课程展示;②深入理解事件驱动编程、状态管理、碰撞检测、定时刷新等基础编程概念;③掌握Python + Pygame开发2D小游戏的核心技能,并具备二次开发与功能拓展能力。; 阅读建议:建议读者按照“规则→设计→代码→运行”的顺序逐步学习,动手实践每个模块并调试代码,重点关注方向缓冲、状态机、食物生成算法等设计细节,同时可尝试实现文末提出的拓展功能以增强项目亮点。

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧