新聞中心
在Silverlight 3.0 RTW新特性中,展示一個(gè)鼠標(biāo)滾動(dòng)事件的示例只需要幾行代碼即可,我認(rèn)為大部分人都可以在幾分鐘內(nèi)做好,并理解它是如何工作的。因此我決定將這個(gè)事件和Expression Blend中引入的新行為一起合并成一個(gè)例子進(jìn)行介紹。

創(chuàng)新互聯(lián)公司專(zhuān)注于永和網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供永和營(yíng)銷(xiāo)型網(wǎng)站建設(shè),永和網(wǎng)站制作、永和網(wǎng)頁(yè)設(shè)計(jì)、永和網(wǎng)站官網(wǎng)定制、重慶小程序開(kāi)發(fā)公司服務(wù),打造永和網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供永和網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。
行為是什么?
你可能曾經(jīng)在ASP.NET Ajax框架中使用過(guò)行為,說(shuō)得簡(jiǎn)單點(diǎn)這里的行為就是ASP.NET Ajax語(yǔ)法的Silverlight實(shí)現(xiàn),允許創(chuàng)建可復(fù)用的和可連接到HTML控件的行為。(Silverlight 3.0 RTW新特性讓操作簡(jiǎn)單的和手工具)
從Blend 3 Beta版開(kāi)始引入行為的概念,可以在設(shè)計(jì)窗口中拖動(dòng)內(nèi)置的行為,增加圖形元素的活力,進(jìn)入Asset文件夾,在這里可以找到控件、效果、資源和其它東西,現(xiàn)在又多了一個(gè)行為卡片。
Expression Blend 3.0 引入了許多行為類(lèi)型,行為< T>是其中最簡(jiǎn)單的了,適用于DependencyObject,行為可以修改控件的外觀,添加元素,修改屬性或處理一個(gè)或多個(gè)事件。MouseDragElementBehavior就是一個(gè)活生生的例子,它連接鼠標(biāo)事件,讓元素可以在頁(yè)面中拖動(dòng)。
編寫(xiě)一個(gè)行為
編寫(xiě)一個(gè)行為是一件很簡(jiǎn)單的事情,行為是行為< T>的類(lèi)擴(kuò)展,因此首先要做的是引用C:\Program Files\Microsoft SDKs\Expression\Blend 3\Interactivity\Libraries\Silverlight目錄下的Microsoft.Expression.Interactions.dll和System.Windows.Interactivity.dll。如果你從Blend 3.0添加一個(gè)現(xiàn)有的行為,那這些動(dòng)態(tài)庫(kù)會(huì)自動(dòng)引用到項(xiàng)目中。
引用添加好后,就可以創(chuàng)建類(lèi)了:
- public class MouseWheelScrollBehavior : Behavior< Control>
- {
- // 在這里添加實(shí)現(xiàn)代碼
- }
由于我們是要擴(kuò)展Silverlight中可滾動(dòng)的組件,我們需要?jiǎng)?chuàng)建一個(gè)可以連接到Control類(lèi)的類(lèi)型,在Silverlight中沒(méi)有通用的用于可滾動(dòng)組件(如ScrollViewer、DataGrid和DataGrid)的類(lèi),這就需要自己想辦法處理才行,我們將在后面進(jìn)行介紹,目前先分析一下如何創(chuàng)建一個(gè)行為。(微軟Silverlight中加入Smooth Streaming)
接下來(lái)要做的是在目標(biāo)對(duì)象上連接MouseWheel事件,當(dāng)我們完成行為類(lèi)的擴(kuò)展后,我們有兩個(gè)辦法來(lái)處理連接和釋放目標(biāo)上的行為:將行為連接到對(duì)象上時(shí)調(diào)用OnAttached,釋放對(duì)象上的行為時(shí)使用OnDetaching。OnAttached和OnDetaching是連接和釋放公共事件的理想選擇,目標(biāo)對(duì)象是通過(guò)行為< T>在AssociatedObject屬性上暴露的,下面是我的代碼示例:
- /// < summary>
- /// Called after the behavior is attached to an AssociatedObject.
- /// < /summary>
- /// < remarks>Override this to hook up functionality to the AssociatedObject.< /remarks>
- protected override void OnAttached()
- {
- this.AssociatedObject.MouseWheel += new MouseWheelEventHandler(AssociatedObject_MouseWheel);
- base.OnAttached();
- }
- /// < summary>
- /// Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.
- /// < /summary>
- /// < remarks>Override this to unhook functionality from the AssociatedObject.< /remarks>
- protected override void OnDetaching()
- {
- this.AssociatedObject.MouseWheel -= new MouseWheelEventHandler(AssociatedObject_MouseWheel);
- base.OnDetaching();
- }
現(xiàn)在行為已經(jīng)準(zhǔn)備好連接到對(duì)象,但它沒(méi)有做任何事情,我們需要為可滾動(dòng)組件實(shí)現(xiàn)滾動(dòng)。
滾動(dòng)可滾動(dòng)的組件 -- 并非如此簡(jiǎn)單
由于沒(méi)有通用的滾動(dòng)接口,即使為ScrollViewer創(chuàng)建一個(gè)行為比較簡(jiǎn)單,但為DataGrid或ListBox創(chuàng)建滾動(dòng)行為卻并不簡(jiǎn)單。
我在http://blog.thekieners.com/2009/04/06/how-to-enable-mouse-wheel-scrolling-in-silverlight-without-extending-controls/發(fā)現(xiàn)有人曾經(jīng)寫(xiě)過(guò)一篇文章介紹如何使用Automation API而不擴(kuò)展控件實(shí)現(xiàn)鼠標(biāo)滾動(dòng)。大家可以去了解一下。這里我們需要知道的是Automation API提供了一個(gè)IScrollProvider接口,因此我們需要修改OnAttached方法,為連接對(duì)象創(chuàng)建Automation Peer。
- /// < summary>
- /// Gets or sets the peer.
- /// < /summary>
- /// < value>The peer.< /value>
- private AutomationPeer Peer { get; set; }
- /// < summary>
- /// Called after the behavior is attached to an AssociatedObject.
- /// < /summary>
- /// < remarks>Override this to hook up functionality to the AssociatedObject.< /remarks>
- 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();
- }
如果控件已經(jīng)創(chuàng)建了自動(dòng)化接口,我們首先來(lái)研究一下它,如果接口不存在,我們需要先創(chuàng)建,AutomationPeer作為一個(gè)成員屬性保存,使用MouseWheel事件時(shí)會(huì)使用到它,下面是滾動(dòng)目標(biāo)對(duì)象的示例代碼:
- /// < summary>
- /// Handles the MouseWheel event of the AssociatedObject control.
- /// < /summary>
- /// < param name="sender">The source of the event.< /param>
- /// < param name="e">The < see cref="System.Windows.Input.MouseWheelEventArgs"/> instance containing the event data.< /param>
- 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);
- }
- }
我們獲取了Delta后需要提取出滾動(dòng)的方向,否則我們就不能指定目標(biāo)滾動(dòng)的像素?cái)?shù)量,但ScrollAmount可能是SmallIncrement或SmallDecrement(或LargeIncrement,LargeDecrement),因此使用方向我們可以確定是遞增還是遞減。
由于控件既可以橫向滾動(dòng)也可以縱向滾動(dòng),我決定檢查換檔鍵是否被按下,如果按下就實(shí)現(xiàn)橫向滾動(dòng),***在IScrollProvider中使用Scroll方法,不需要檢查邊界。
使用行為
使用Blend應(yīng)用行為的操作非常簡(jiǎn)單,Blend資產(chǎn)庫(kù)會(huì)掃描項(xiàng)目中所有的類(lèi),并顯示出來(lái),因此只需要拖動(dòng)行為到滾動(dòng)組件上就可以應(yīng)用行為了,我們需要學(xué)習(xí)的是通過(guò)編碼應(yīng)用行為,下面是一個(gè)例子:
- < UserControl
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
- xmlns:local="clr-namespace:Elite.Silverlight3.MouseWheelSample.Silverlight.Classes"
- xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
- x:Class="Elite.Silverlight3.MouseWheelSample.Silverlight.MainPage"
- Width="Auto" Height="Auto">
- ... omissis ...
- < data:DataGrid Grid.Column="" Grid.Row="0" ItemsSource="{Binding DataItems}" Margin="20">
- < i:Interaction.Behaviors>
- < local:MouseWheelScrollBehavior />
- < /i:Interaction.Behaviors>
- < /data:DataGrid>
在UserControl中,我們聲明了要使用的命名空間,在這個(gè)例子中,"i"代表交互,"local"指的是融入了新行為的本地類(lèi),在第二部分中我們將行為連接到DataGrid了,將行為連接到ScrollViewer或ListBox的代碼非常類(lèi)似,運(yùn)行這個(gè)項(xiàng)目,我們?cè)贒ataGrid上就可以使用鼠標(biāo)滾輪了。
Silverlight 3.0 RTW新特性小結(jié)
本文介紹的Silverlight 3.0 RTW新特性非常有實(shí)用價(jià)值,幾乎適用于所有的可滾動(dòng)控件,但ComboBox是個(gè)例外,因?yàn)樗鼪](méi)有直接實(shí)現(xiàn)IScrollProvider接口,缺點(diǎn)是只能工作在Windows上,這是一個(gè)較大的問(wèn)題,但目前并沒(méi)有解決辦法,因?yàn)樗悄壳巴ㄟ^(guò)編程實(shí)現(xiàn)滾動(dòng)的唯一方法,此外我還注意到MouseWheel事件只能在Windows下IE和Firefox (非Windows模式)中工作,這是由Safari和Firefox 的架構(gòu)決定的,唯一變通的方法是使用DOM事件。
網(wǎng)頁(yè)題目:詳解Silverlight3.0RTW新特性
分享鏈接:http://www.fisionsoft.com.cn/article/djccppj.html


咨詢
建站咨詢
