SilverLight:在MVVM中实现多事件

SilverLight:在MVVM中实现多事件,第1张

概述转载于 http://www.cnblogs.com/888h/archive/2010/12/08/1900621.html 在开发Silverlight项目时,如果使用了MVVM架构时,可以实现业务逻辑与界面的完全分离。事件可以通过实现接口ICommand达到效果,比如:Button控件,如果要实现单击效果时,可以通过绑定Command即可。   但是如果需要实现鼠标离开Button事件怎么实

转载于 http://www.cnblogs.com/888h/archive/2010/12/08/1900621.html

在开发Silverlight项目时,如果使用了MVVM架构时,可以实现业务逻辑与界面的完全分离。事件可以通过实现接口ICommand达到效果,比如:button控件,如果要实现单击效果时,可以通过绑定Command即可。

  但是如果需要实现鼠标离开button事件怎么实现呢,就这是今天需要讨论的问题=》多事件实现

  项目架构如下图:

  

  我今天主要用button做实验,来实现button控件的单击事件和鼠标离开事件。这在非MVVM架构下非常容易实现。但是在MVVM架构,我们需要引用System.windows.Interactivity.dll,此动态库存放的位置为C:\Program files\Microsoft SDKs\Expression\Blend 3\Interactivity\librarIEs\Silverlight\System.windows.Interactivity.dll

  关于System.windows.Interactivity.dll的介绍,请查看http://msdn.microsoft.com/zh-cn/library/system.windows.interactivity(v=Expression.40).aspx

  通过引用动态库,然后在MainPage.xaml中实现button的两个事件。代码如下:

  MainPage.xaml

<UserControl x:Class="MoreEvent.MainPage"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d
="http://schemas.microsoft.com/Expression/blend/2008"
    xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
    
xmlns:i="http://schemas.microsoft.com/Expression/2010/interactivity"
    xmlns:local
="clr-namespace:MoreEventviewmodel;assembly=MoreEventviewmodel"
    mc:Ignorable
="d"
    d:DesignHeight
="300" d:DesignWIDth="400">
    
<UserControl.Resources>
        
<local:MoreEventsviewmodel x:Key="k"/>
    
</UserControl.Resources>
    
<GrID x:name="LayoutRoot" Background="White" DataContext="{StaticResource k}">
        
<button Content="测试多事件" WIDth="70" Height="25">
            
<i:Interaction.Triggers>
                
i:EventTrigger EventnameClick">
                    
i:InvokeCommandAction Command{Binding BtnClick}/>
                
</i:EventTriggerMouseLeave{Binding MouseLeave}>
            
>
        
</button>
    
</GrID>
</UserControl>

那么viewmodel和ICommand的实现非常简单,两个文件的代码如下

  MoreEventsviewmodel.cs

using System;
using System.Net;
using System.windows;
using System.windows.Controls;
using System.windows.documents;
using System.windows.Ink;
using System.windows.input;
using System.windows.Media;
using System.windows.Media.Animation;
using System.windows.Shapes;

namespace MoreEventviewmodel
{
    
public class MoreEventsviewmodel
    {
        
public MoreEventsviewmodel()
        { 
        
        }

        
private voID MouseLeaveEvent(object obj)
        {
            MessageBox.Show(
"测试鼠标离开事件");
        }
        
private voID BtnClickEvent(object obj)
        {
            MessageBox.Show(
"测试单击事件");
        }

        
public ICommand MouseLeave
        {
            
get { return new MoreEventCommand(MouseLeaveEvent); }
        }
        
public ICommand BtnClick
        {
            
get { return new MoreEventCommand(BtnClickEvent); }
        }
    }
}
MoreEventCommand.cs

using System;
using System.Net;
using System.windows;
using System.windows.Controls;
using System.windows.documents;
using System.windows.Ink;
using System.windows.input;
using System.windows.Media;
using System.windows.Media.Animation;
using System.windows.Shapes;

namespace MoreEventviewmodel
{
    
public class MoreEventCommand:ICommand
    {
        Action
<object> _action;
        
public MoreEventCommand(Action<object> ac)
        {
            _action 
= ac;
        }
        
public bool CanExecute(object parameter)
        {
            
return true;
        }

        
public event EventHandler CanExecuteChanged;

        
public voID Execute(object parameter)
        {
            
if (_action != null)
                _action(parameter);
        }
    }
}

通过以上方法即可在MVVM实现多事件.通过这个件事,大家可以触类旁通,实现其它控件的多事件。希望对大家有用。

点击下载源程序

总结

以上是内存溢出为你收集整理的SilverLight:在MVVM中实现多事件全部内容,希望文章能够帮你解决SilverLight:在MVVM中实现多事件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1020359.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存