代码改变世界

SilverLight4 新特性(一)

2010-03-28 23:06  小黑哥  阅读(656)  评论(0编辑  收藏  举报

摘要:

       微软在MIX10大会上发布了SilverLight4 RC版,在这个版本里包含了大量的新特性,最近计划升级程序,需要系统学习一下SilverLight4,让我们一起开始吧...

内容:

      鼠标滚轮支持

      在SilverLight4之前,要实现鼠标滚轮是一件不容易得事情,主要利用HTML DOM 事件,实现起来很不容易,后来找了一个相对来说比较好的办法,方法如下:

    public class MouseWheelScrollBehavior : Behavior<Control>
    {
        private AutomationPeer Peer { get; set; }

        protected override void OnAttached()
        {
            this.Peer = FrameworkElementAutomationPeer.FromElement(this.AssociatedObject);
            if (this.Peer == null)
                this.Peer = FrameworkElementAutomationPeer.CreatePeerForElement(this.AssociatedObject);
            this.AssociatedObject.MouseWheel += new MouseWheelEventHandler(AssociatedObject_MouseWheel);
            base.OnAttached();
        }

        protected override void OnDetaching()
        {
            this.AssociatedObject.MouseWheel -= new MouseWheelEventHandler(AssociatedObject_MouseWheel);
            base.OnDetaching();
        }

        void AssociatedObject_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            this.AssociatedObject.Focus();
            int direction = Math.Sign(e.Delta);
            ScrollAmount scrollAmount =
                (direction < 0) ? ScrollAmount.SmallIncrement : ScrollAmount.SmallDecrement;
            if (this.Peer != null)
            {
                IScrollProvider scrollProvider =
                    this.Peer.GetPattern(PatternInterface.Scroll) as IScrollProvider;
                bool shiftKey = (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift;
                if (scrollProvider != null && scrollProvider.VerticallyScrollable && !shiftKey)
                    scrollProvider.Scroll(ScrollAmount.NoAmount, scrollAmount);
                else if (scrollProvider != null && scrollProvider.VerticallyScrollable && shiftKey)
                    scrollProvider.Scroll(scrollAmount, ScrollAmount.NoAmount);
            }
        }
    }
    DataGrid中使用
    <data:DataGrid>
       <i:Interaction.Behaviors> 
         <my:MouseWheelScrollBehavior />
       </i:Interaction.Behaviors>
    </data:DataGrid>


      SilverLight4中,UIElement 提供了原生的MouseWheel支持,DataGrid、ListBox、ScrollViewer等不用实现任何代码就可以实现鼠标滚轮。以下是测试代码,点击按钮后就可以尽情的用滚轮了!!!

      Xaml文件:

<UserControl xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="SilverlightApplication3.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"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="240"/>
            <RowDefinition Height="28"/>
        </Grid.RowDefinitions>
        <my:DataGrid x:Name="dataGridUsers" Margin="20" AutoGenerateColumns="True"/>
        <Button x:Name="buttonGetData" Grid.Row="1" Content="GetData" Margin="20,0,20,0" Click="buttonGetData_Click" />
    </Grid>
</UserControl>

 

向DataGird中添加100条数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication3
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void buttonGetData_Click(object sender, RoutedEventArgs e)
        {
            List<User> users = new List<User>();
            for (int i = 0; i < 100; i++)
            {
                users.Add(new User() { Number = i,UserName = string.Format("用户{0}", i) });
            }
            this.dataGridUsers.ItemsSource = users;
        }
    }

    public class User
    {
        public string UserName { get; set; }
        public int Number { get; set; }
    }
}
MouseWheel API 中的 Delta

当鼠标滚轮事件注册了后,它会设置e.Delta作为鼠标滚动的输出值,这个值向前为正,向后为负。每次都是正负120或其倍数。这个值和WPF中的值保持一致。可以通过这个值设置你程序的逻辑,可用于屏幕的缩放。

 

结语:
    SilverLight2、3中不支持鼠标滚轮,想了很多办法,最后添加了“MouseWheelScrollBehavior”才勉强支持。SilverLight4中原生支持了,我怎么也高兴不起来。要删除近百处这样的Behavior不比当时添加容易!!!     
 
说明:
    以上代码在VS2010RC +  SilverLight4 RC 调试通过