SilverLight:在MVVM中實現多事件

在開發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 EventName = " Click " >
                    
< i:InvokeCommandAction Command = " {Binding BtnClick} " />
                
</ i:EventTrigger >
                
< i:EventTrigger EventName = " MouseLeave " >
                    
< i:InvokeCommandAction Command = " {Binding MouseLeave} " />
                
</ i:EventTrigger >
            
</ i:Interaction.Triggers >
        
</ 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實現多事件.通過這個件事,大家可以觸類旁通,實現其它控件的多事件。希望對大家有用。

本文來自天神一的博客,原文地址:http://www.cnblogs.com/888h/archive/2010/12/08/1900621.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章