WPF省略號動畫_第1頁
WPF省略號動畫_第2頁
WPF省略號動畫_第3頁
WPF省略號動畫_第4頁
WPF省略號動畫_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、WPF 省略號動畫省略號動畫可以用于加載數(shù)據(jù),等待等多個場景使用,在wpf中要實現(xiàn)省略號動畫方式多種多樣,但是要通用起來目前筆者還沒有看到一個好的結(jié)局方案?,F(xiàn)在我們一起來做一個通用的省略號動畫類,達到像拖控件一樣簡單。思路,有一個類,我只要實例化它后,傳入一個UI元素比如Border或Panel。調(diào)用某個方法比如Run,就可以讓小圓點跑起來。首先建立一個用來調(diào)用的類,取名為:EllipsisRun調(diào)用類創(chuàng)建完成。省略號。是6個小圓點。那么我就需要在類里面來創(chuàng)建6個小圓點。小圓點創(chuàng)建好了,那么怎么讓小圓點看起來似乎在動呢?通過設(shè)置小圓點的樣式來實現(xiàn),比如:先定義三個個樣式字段:1. Ellips

2、eStyleBase用來設(shè)置小圓點的Height,Width,Margin;2. EllipseStyleNormal繼承EllipseStyleBase追加Stroke(設(shè)置邊框顏色)和StrokeThickness(設(shè)置邊框顯示大?。?;3. EllipseStyleHilight繼承EllipseStyleBase追加Fill(設(shè)置填充顏色)所以我們需要一個方法來設(shè)置這三個樣式:我們想設(shè)置個默認值怎么樣,同時又想自己定義下值,那么我可以用一個方法來設(shè)置。我這里使用構(gòu)造方法來實現(xiàn)。小圓點樣式已經(jīng)完成了,那么我們現(xiàn)在需要讓小圓點“動起來”,我們需要創(chuàng)建一個Run方法,傳入我們指定的容器,讓校園

3、的在這個容器中“動起來”。開來我們還需要添加一些關(guān)鍵性的字段和屬性現(xiàn)在小圓點可以跑起來了。在情景中,我們數(shù)據(jù)加載完了。還需要關(guān)閉動畫。所以我們還需要一個退出動畫的方法來調(diào)用,這里我們用End()方法來實現(xiàn)。好的大功告我們來調(diào)用下我們的小圓點動畫XAMLCS:運行起來看上去還不錯哦。下面是完整代碼:/ <summary> / 省略號動畫 / 用法: / var er = new EllipsisRun(); / er.Run(參數(shù):Border或Panel); / </summary> public class EllipsisRun private Style Elli

4、pseStyleBase; private Style EllipseStyleNormal; private Style EllipseStyleHilight; private UIElement _Content; / <summary> / 要插入動畫的UI元素 Border或Panel / </summary> public UIElement TargetParent get; private set; / <summary> / true動畫正在進行中 false動畫未開始或者已經(jīng)結(jié)束 / </summary> public boo

5、l IsRun get; private set; / <summary> / 實例化 省略號動畫動畫對象 / </summary> public EllipsisRun() double hw = 8, m = 2, s = 1; init(hw, hw, m, s, Brushes.BlueViolet); / <summary> / 實例化 省略號動畫動畫對象 / height省略號元素高 / width省略號元素寬 / margion省略號元素邊距 / strokeThickness省略號元素裱框 / brush省略號元素填充色 / </sum

6、mary> / <param name="height">省略號元素高</param> / <param name="width">省略號元素寬</param> / <param name="margion">省略號元素邊距</param> / <param name="strokeThickness">省略號元素裱框</param> / <param name="brush">省略

7、號元素裱框</param> public EllipsisRun(double height, double width, double margion, double strokeThickness, Brush brush) init(height, width, margion, strokeThickness, brush); private void init(double h, double w, double m, double s, Brush brush) EllipseStyleBase = new Style(typeof(Ellipse); var s_he

8、ight = new Setter(Ellipse.HeightProperty, h); var s_width = new Setter(Ellipse.WidthProperty, w); var s_margin = new Setter(Ellipse.MarginProperty, new Thickness(m); EllipseStyleBase.Setters.Add(s_height); EllipseStyleBase.Setters.Add(s_width); EllipseStyleBase.Setters.Add(s_margin); EllipseStyleNor

9、mal = new Style(typeof(Ellipse), EllipseStyleBase); var s_stroke = new Setter(Ellipse.StrokeProperty, brush); var s_stroke_thickness = new Setter(Ellipse.StrokeThicknessProperty, s); EllipseStyleNormal.Setters.Add(s_stroke); EllipseStyleNormal.Setters.Add(s_stroke_thickness); EllipseStyleHilight = n

10、ew Style(typeof(Ellipse), EllipseStyleBase); var s_fill = new Setter(Ellipse.FillProperty, brush); EllipseStyleHilight.Setters.Add(s_fill); / <summary> / 開始動畫 / targetUI 指定要進行動畫UI元素(Border或Panel) / 異常: / 1.動畫進行中 / 2.targetUI為null或非Border、Panel元素 / </summary> / <param name="target

11、UI">指定要進行動畫UI元素(Border或Panel)</param> / <returns></returns> public EllipsisRun Run(UIElement targetUI) if (IsRun) throw new Exception("目標(biāo)正在使用中"); List<Ellipse> items = null; var parent = createEllipseItems(out items); if (targetUI is Border) (targetUI as Bor

12、der).Child = parent; else if (targetUI is Panel) (targetUI as Panel).Children.Add(parent); else throw new Exception("目標(biāo)ui只能是Panel或Border類型"); TargetParent = targetUI; _Content = parent; IsRun = true; var task = Task.Factory.StartNew() => int index = 0; while (IsRun) if (index >= item

13、s.Count) index = 0; var index_last = index - 1; if (index_last < 0) index_last = items.Count - 1; var item_last = itemsindex_last; var item_next = itemsindex; targetUI.Dispatcher.Invoke() => item_last.Style = EllipseStyleNormal; item_next.Style = EllipseStyleHilight; ); index+; Thread.Sleep(12

14、0); ); return this; / <summary> / 結(jié)束正在進行的動畫 / 異常: / 1.動畫未進行 / 2.動畫已經(jīng)結(jié)束 / </summary> / <returns></returns> public EllipsisRun End() if (IsRun) IsRun = false; Thread.Sleep(200); if (TargetParent is Border) (TargetParent as Border).Child = null; else if (TargetParent is Panel) (TargetParent as Panel).Children.Remove(_Content); return this; else throw new Exception("目標(biāo)已經(jīng)停止使用或未開始"); private WrapPanel createEllipseItems(out List<Ellipse> list) list = new List<Ellipse>(); var parent = new Wrap

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論