惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
MyScale Blog
MyScale Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
爱范儿
爱范儿
P
Proofpoint News Feed
人人都是产品经理
人人都是产品经理
Last Week in AI
Last Week in AI
罗磊的独立博客
G
Google Developers Blog
Y
Y Combinator Blog
博客园 - 【当耐特】
WordPress大学
WordPress大学
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
J
Java Code Geeks
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Visual Studio Blog
美团技术团队
宝玉的分享
宝玉的分享
Jina AI
Jina AI
小众软件
小众软件
T
Tailwind CSS Blog
A
About on SuperTechFans

博客园 - 天神一

Windows 8的本地化应用程序清单 代码滑动panorama-即程序中设置SelectedIndex WP7开发 Sqlite数据库的使用 解决Unable open the database WP7 真机调试PhotoChooserTask和CameraCaptureTask 解决windows8不能安装ZUNE的问题 软件概要设计 AutoResetEvent详解 解决VS2008 调试启动特别慢 微软WP7本地数据库之Sqlite编程技巧(转) 桥接模式的简单分析 代码的“门面”——模式系列谈之Facade模式(转) SilverLight4:在MVVM架构下实现模式窗口 简析SilverLight的List<T>、ObservableCollection<T>和INotifyPropertyChanged的异同 MVVM架构的简单解析 在MVVM架构下,把EventArgs绑定到Command上【翻译】 XNA 4.0新书:初学者很好的书籍 SilverLight:使用MVVM实现View层在程序运行时自动生成控件并且取得其值 (翻译)在MVVM模式中打开子窗体(Child Window) Windows Phone 7 完全版电子书可以免费下载了
SilverLight:在MVVM中实现多事件
天神一 · 2010-12-08 · via 博客园 - 天神一

  在开发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实现多事件.通过这个件事,大家可以触类旁通,实现其它控件的多事件。希望对大家有用。

  点击下载源程序