微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight 解谜游戏 之七 放大镜3

       在前两篇文章中,我们已经创建了“放大镜效果”和“放大镜CheckBox”,本篇内容将通过CheckBox来控制放大镜效果的开/关状态,并完成全部“放大镜”功能。在进行本篇内容前请确保您已经阅读过:

Silverlight 解谜游戏 之五 放大镜(1)

Silverlight 解谜游戏 之六 放大镜(2)

 

在完成本篇内容后将实现以下效果

Get Microsoft Silverlight

 

1. 下载BaseBehavior.zip 进入VS 将以下程序加入到Interactivity 文件夹(参考资料 Base Classes for Custom Behaviors

    ・ BaseBehavior.cs 
    ・ BaseBehaviorT.cs

             

 

2. 下载SetInteractionPropertyAction.zip 新增Interactivity\SetInteractionPropertyAction 文件夹,并将以下程序加入其中(参考资料 Creating an Action to set Properties on Actions & Behaviors)

    ・ ConverterHelper.cs
    ・ SetInteractionPropertyAction.cs

            

 

添加完以上四个程序,注意其命名空间应为:Findobject

file

其中,ConverterHelper.cs 和SetInteractionPropertyAction.cs 程序需要使用CodePlex 的Expression Blend Samples 项目,下载安装后将Expression.Samples.InteractivityExpression.Samples.Shaders 加入Reference:

reference

3. 打开MagnifierOverBehavior.cs 要使MagnifierOverBehavior 继承BaseBehavior 类,以此来使用IsEnabled 属性。将Behavior 改为BaseBehavior

public class MagnifierOverBehavior : BaseBehavior<FrameworkElement>

将Associatedobject_MouseEnter 显示放大镜注释掉,需要通过IsEnabled 来进行调用

private void Associatedobject_MouseEnter( object sender,MouseEventArgs e )  {     this.Associatedobject.MouseMove += 
new MouseEventHandler( Associatedobject_MouseMove ); //this.Associatedobject.Effect = this.magnifier; }

Associatedobject_MouseMove 也要通过IsEnabled 来判断是否开启放大镜功能

private void Associatedobject_MouseMove( object sender,MouseEventArgs e )  {     if (IsEnabled) { if (this.Associatedobject.Effect != this.magnifier) { this.Associatedobject.Effect = this.magnifier; }          (this.Associatedobject.Effect as Magnifier).Center =         e.GetPosition(this.Associatedobject);          Point mousePosition = e.GetPosition(this.Associatedobject);        mousePosition.X /= this.Associatedobject.ActualWidth;        mousePosition.Y /= this.Associatedobject.ActualHeight;        this.magnifier.Center = mousePosition;          Storyboard zoomInStoryboard = new Storyboard();        DoubleAnimation zoomInAnimation = new DoubleAnimation();        zoomInAnimation.To = this.magnifier.Magnification;        zoomInAnimation.Duration = TimeSpan.FromSeconds(0.5);        Storyboard.SetTarget(zoomInAnimation,this.Associatedobject.Effect);        Storyboard.SetTargetProperty(zoomInAnimation,
new PropertyPath(Magnifier.MagnificationProperty)); zoomInAnimation.FillBehavior = FillBehavior.HoldEnd; zoomInStoryboard.Children.Add(zoomInAnimation); zoomInStoryboard.Begin(); } }

4. 回到Blend,在TreeView 中打开magnifierCanvas 选择MagnifierOverBehavior 将其命名为magnifierBehavior,并将IsEnabled 属性设为false

isenable

5. 在Assets->Behaviors 中为CheckBox 添加两个SetInteractionPropertyAction

setinertaction

checkbox

6. 第一个SetInteractionPropertyAction 用于启动放大镜功能,进行如下设置:

    ・ EventName: Checked
    ・ TargetName: magnifierCanvas
    ・ ObjectName: magnifierBehavior
    ・ PropertyName: IsEnabled
    ・ Value: true

open

7. 第二个SetInteractionPropertyAction 用于关闭放大镜功能,进行如下设置:

    ・ EventName: Unchecked
    ・ TargetName: magnifierCanvas
    ・ ObjectName: magnifierBehavior
    ・ PropertyName: IsEnabled
    ・ Value: false

close

8. 确定CheckBoxIsChecked 属性false 状态,运行程序便可实现放大镜可控效果

ischecked

9. 最后在放大镜下面加一个Notepad 图片,使其更加美观:

notepad

代码下载:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐