超炫酷的WPF完成Loading控件後果。本站提示廣大學習愛好者:(超炫酷的WPF完成Loading控件後果)文章只能為提供參考,不一定能成為您想要的結果。以下是超炫酷的WPF完成Loading控件後果正文
Win8體系的Loading後果照樣很不錯的,網上也有人用CSS3等技巧完成,研討了一下,並盤算用WPF自界說一個Loading控件完成相似的後果,並可讓用戶對Loading的顆粒(Particle)配景色彩停止自界說,話不多說,直接上代碼:
1、用VS2012新建一個WPF的用戶控件庫項目WpfControlLibraryDemo,VS主動生成以下構造:
2、刪除UserControl1.xaml,並新建一個Loading的CustomControl(不是UserControl),以下圖所示:
3、假如報錯找不到Loading類型,請編譯,上面在Generic.xaml主題文件中對Loading的款式和內容停止界說(留意添加
xmlns:system = "clr-namespace:System;assembly=mscorlib"),代碼以下: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:system = "clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:WpfControlLibraryDemo"> <Style TargetType="{x:Type local:Loading}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:Loading}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <Grid Width = "50" Height = "50"> <Grid.Resources> <!-- Value Converters --> <!-- Particle Styling ,must to has RelativeSource --> <SolidColorBrush x:Key = "ParticleColor" Color = "{Binding Path=FillColor,RelativeSource={RelativeSource TemplatedParent}}" /> <SolidColorBrush x:Key = "ParticleBackgroundColor" Color = "Transparent"/> <system:Double x:Key = "ParticleOpacity">1</system:Double> <system:Double x:Key = "ParticleRadius">5</system:Double> <system:Double x:Key = "StartingPointX">0</system:Double> <system:Double x:Key = "StartingPointY">-20</system:Double> <system:Double x:Key = "RotationPointX">0.5</system:Double> <system:Double x:Key = "RotationPointY">0.5</system:Double> <!-- StoryBoard --> <system:TimeSpan x:Key = "StoryBoardBeginTimeP0">00:00:00.000</system:TimeSpan> <system:TimeSpan x:Key = "StoryBoardBeginTimeP1">00:00:00.100</system:TimeSpan> <system:TimeSpan x:Key = "StoryBoardBeginTimeP2">00:00:00.200</system:TimeSpan> <system:TimeSpan x:Key = "StoryBoardBeginTimeP3">00:00:00.300</system:TimeSpan> <system:TimeSpan x:Key = "StoryBoardBeginTimeP4">00:00:00.400</system:TimeSpan> <Duration x:Key = "StoryBoardDuration">00:00:01.800</Duration> <!-- Particle Origin Angles --> <system:Double x:Key = "ParticleOriginAngleP0">0</system:Double> <system:Double x:Key = "ParticleOriginAngleP1">-10</system:Double> <system:Double x:Key = "ParticleOriginAngleP2">-20</system:Double> <system:Double x:Key = "ParticleOriginAngleP3">-30</system:Double> <system:Double x:Key = "ParticleOriginAngleP4">-40</system:Double> <!-- Particle Position & Timing 1 --> <system:Double x:Key = "ParticleBeginAngle1">0</system:Double> <system:Double x:Key = "ParticleEndAngle1">90</system:Double> <system:TimeSpan x:Key = "ParticleBeginTime1">00:00:00.000</system:TimeSpan> <Duration x:Key = "ParticleDuration1">00:00:00.750</Duration> <!-- Particle Position & Timing 2 --> <system:Double x:Key = "ParticleBeginAngle2">90</system:Double> <system:Double x:Key = "ParticleEndAngle2">270</system:Double> <system:TimeSpan x:Key = "ParticleBeginTime2">00:00:00.751</system:TimeSpan> <Duration x:Key = "ParticleDuration2">00:00:00.300</Duration> <!-- Particle Position & Timing 3 --> <system:Double x:Key = "ParticleBeginAngle3">270</system:Double> <system:Double x:Key = "ParticleEndAngle3">360</system:Double> <system:TimeSpan x:Key = "ParticleBeginTime3">00:00:01.052</system:TimeSpan> <Duration x:Key = "ParticleDuration3">00:00:00.750</Duration> <Style x:Key = "EllipseStyle" TargetType = "Ellipse"> <Setter Property = "Width" Value = "{StaticResource ParticleRadius}"/> <Setter Property = "Height" Value = "{StaticResource ParticleRadius}"/> <Setter Property = "Fill" Value = "{StaticResource ParticleColor}"/> <Setter Property = "RenderTransformOrigin" Value = "0.5, 0.5"/> <Setter Property = "Opacity" Value = "{StaticResource ParticleOpacity}"/> </Style> </Grid.Resources> <Canvas Width = "1" Height = "1" Margin="0,0,0,0"> <Canvas.Triggers> <EventTrigger RoutedEvent = "Canvas.Loaded"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard BeginTime = "{StaticResource StoryBoardBeginTimeP0}" Duration = "{StaticResource StoryBoardDuration}" RepeatBehavior = "Forever"> <DoubleAnimation Storyboard.TargetName = "p0" Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)" From = "{StaticResource ParticleBeginAngle1}" To = "{StaticResource ParticleEndAngle1}" BeginTime = "{StaticResource ParticleBeginTime1}" Duration = "{StaticResource ParticleDuration1}"/> <DoubleAnimation Storyboard.TargetName = "p0" Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)" From = "{StaticResource ParticleBeginAngle2}" To = "{StaticResource ParticleEndAngle2}" BeginTime = "{StaticResource ParticleBeginTime2}" Duration = "{StaticResource ParticleDuration2}"/> <DoubleAnimation Storyboard.TargetName = "p0" Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)" From = "{StaticResource ParticleBeginAngle3}" To = "{StaticResource ParticleEndAngle3}" BeginTime = "{StaticResource ParticleBeginTime3}" Duration = "{StaticResource ParticleDuration3}"/> </Storyboard> </BeginStoryboard> <BeginStoryboard> <Storyboard BeginTime = "{StaticResource StoryBoardBeginTimeP1}" Duration = "{StaticResource StoryBoardDuration}" RepeatBehavior = "Forever"> <DoubleAnimation Storyboard.TargetName = "p1" Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)" From = "{StaticResource ParticleBeginAngle1}" To = "{StaticResource ParticleEndAngle1}" BeginTime = "{StaticResource ParticleBeginTime1}" Duration = "{StaticResource ParticleDuration1}"/> <DoubleAnimation Storyboard.TargetName = "p1" Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)" From = "{StaticResource ParticleBeginAngle2}" To = "{StaticResource ParticleEndAngle2}" BeginTime = "{StaticResource ParticleBeginTime2}" Duration = "{StaticResource ParticleDuration2}"/> <DoubleAnimation Storyboard.TargetName = "p1" Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)" From = "{StaticResource ParticleBeginAngle3}" To = "{StaticResource ParticleEndAngle3}" BeginTime = "{StaticResource ParticleBeginTime3}" Duration = "{StaticResource ParticleDuration3}"/> </Storyboard> </BeginStoryboard> <BeginStoryboard> <Storyboard BeginTime = "{StaticResource StoryBoardBeginTimeP2}" Duration = "{StaticResource StoryBoardDuration}" RepeatBehavior = "Forever"> <DoubleAnimation Storyboard.TargetName = "p2" Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)" From = "{StaticResource ParticleBeginAngle1}" To = "{StaticResource ParticleEndAngle1}" BeginTime = "{StaticResource ParticleBeginTime1}" Duration = "{StaticResource ParticleDuration1}"/> <DoubleAnimation Storyboard.TargetName = "p2" Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)" From = "{StaticResource ParticleBeginAngle2}" To = "{StaticResource ParticleEndAngle2}" BeginTime = "{StaticResource ParticleBeginTime2}" Duration = "{StaticResource ParticleDuration2}"/> <DoubleAnimation Storyboard.TargetName = "p2" Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)" From = "{StaticResource ParticleBeginAngle3}" To = "{StaticResource ParticleEndAngle3}" BeginTime = "{StaticResource ParticleBeginTime3}" Duration = "{StaticResource ParticleDuration3}"/> </Storyboard> </BeginStoryboard> <BeginStoryboard> <Storyboard BeginTime = "{StaticResource StoryBoardBeginTimeP3}" Duration = "{StaticResource StoryBoardDuration}" RepeatBehavior = "Forever"> <DoubleAnimation Storyboard.TargetName = "p3" Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)" From = "{StaticResource ParticleBeginAngle1}" To = "{StaticResource ParticleEndAngle1}" BeginTime = "{StaticResource ParticleBeginTime1}" Duration = "{StaticResource ParticleDuration1}"/> <DoubleAnimation Storyboard.TargetName = "p3" Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)" From = "{StaticResource ParticleBeginAngle2}" To = "{StaticResource ParticleEndAngle2}" BeginTime = "{StaticResource ParticleBeginTime2}" Duration = "{StaticResource ParticleDuration2}"/> <DoubleAnimation Storyboard.TargetName = "p3" Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)" From = "{StaticResource ParticleBeginAngle3}" To = "{StaticResource ParticleEndAngle3}" BeginTime = "{StaticResource ParticleBeginTime3}" Duration = "{StaticResource ParticleDuration3}"/> </Storyboard> </BeginStoryboard> <BeginStoryboard> <Storyboard BeginTime = "{StaticResource StoryBoardBeginTimeP4}" Duration = "{StaticResource StoryBoardDuration}" RepeatBehavior = "Forever"> <DoubleAnimation Storyboard.TargetName = "p4" Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)" From = "{StaticResource ParticleBeginAngle1}" To = "{StaticResource ParticleEndAngle1}" BeginTime = "{StaticResource ParticleBeginTime1}" Duration = "{StaticResource ParticleDuration1}"/> <DoubleAnimation Storyboard.TargetName = "p4" Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)" From = "{StaticResource ParticleBeginAngle2}" To = "{StaticResource ParticleEndAngle2}" BeginTime = "{StaticResource ParticleBeginTime2}" Duration = "{StaticResource ParticleDuration2}"/> <DoubleAnimation Storyboard.TargetName = "p4" Storyboard.TargetProperty = "(UIElement.RenderTransform).(RotateTransform.Angle)" From = "{StaticResource ParticleBeginAngle3}" To = "{StaticResource ParticleEndAngle3}" BeginTime = "{StaticResource ParticleBeginTime3}" Duration = "{StaticResource ParticleDuration3}"/> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> </Canvas.Triggers> <Border x:Name = "p0" Background = "{StaticResource ParticleBackgroundColor}" Opacity = "{StaticResource ParticleOpacity}"> <Border.RenderTransform> <RotateTransform/> </Border.RenderTransform> <Border.RenderTransformOrigin> <Point X = "{StaticResource RotationPointX}" Y = "{StaticResource RotationPointY}"/> </Border.RenderTransformOrigin> <Ellipse Style = "{StaticResource EllipseStyle}"> <Ellipse.RenderTransform> <TransformGroup> <TranslateTransform X = "{StaticResource StartingPointX}" Y = "{StaticResource StartingPointY}"/> <RotateTransform Angle = "{StaticResource ParticleOriginAngleP0}"/> </TransformGroup> </Ellipse.RenderTransform> </Ellipse> </Border> <Border x:Name = "p1" Background = "{StaticResource ParticleBackgroundColor}" Opacity = "{StaticResource ParticleOpacity}"> <Border.RenderTransform> <RotateTransform/> </Border.RenderTransform> <Border.RenderTransformOrigin> <Point X = "{StaticResource RotationPointX}" Y = "{StaticResource RotationPointY}"/> </Border.RenderTransformOrigin> <Ellipse Style = "{StaticResource EllipseStyle}"> <Ellipse.RenderTransform> <TransformGroup> <TranslateTransform X = "{StaticResource StartingPointX}" Y = "{StaticResource StartingPointY}"/> <RotateTransform Angle = "{StaticResource ParticleOriginAngleP1}"/> </TransformGroup> </Ellipse.RenderTransform> </Ellipse> </Border> <Border x:Name = "p2" Background = "{StaticResource ParticleBackgroundColor}" Opacity = "{StaticResource ParticleOpacity}"> <Border.RenderTransform> <RotateTransform/> </Border.RenderTransform> <Border.RenderTransformOrigin> <Point X = "{StaticResource RotationPointX}" Y = "{StaticResource RotationPointY}"/> </Border.RenderTransformOrigin> <Ellipse Style = "{StaticResource EllipseStyle}"> <Ellipse.RenderTransform> <TransformGroup> <TranslateTransform X = "{StaticResource StartingPointX}" Y = "{StaticResource StartingPointY}"/> <RotateTransform Angle = "{StaticResource ParticleOriginAngleP2}"/> </TransformGroup> </Ellipse.RenderTransform> </Ellipse> </Border> <Border x:Name = "p3" Background = "{StaticResource ParticleBackgroundColor}" Opacity = "{StaticResource ParticleOpacity}"> <Border.RenderTransform> <RotateTransform/> </Border.RenderTransform> <Border.RenderTransformOrigin> <Point X = "{StaticResource RotationPointX}" Y = "{StaticResource RotationPointY}"/> </Border.RenderTransformOrigin> <Ellipse Style = "{StaticResource EllipseStyle}"> <Ellipse.RenderTransform> <TransformGroup> <TranslateTransform X = "{StaticResource StartingPointX}" Y = "{StaticResource StartingPointY}"/> <RotateTransform Angle = "{StaticResource ParticleOriginAngleP3}"/> </TransformGroup> </Ellipse.RenderTransform> </Ellipse> </Border> <Border x:Name = "p4" Background = "{StaticResource ParticleBackgroundColor}" Opacity = "{StaticResource ParticleOpacity}"> <Border.RenderTransform> <RotateTransform/> </Border.RenderTransform> <Border.RenderTransformOrigin> <Point X = "{StaticResource RotationPointX}" Y = "{StaticResource RotationPointY}"/> </Border.RenderTransformOrigin> <Ellipse Style = "{StaticResource EllipseStyle}"> <Ellipse.RenderTransform> <TransformGroup> <TranslateTransform X = "{StaticResource StartingPointX}" Y = "{StaticResource StartingPointY}"/> <RotateTransform Angle = "{StaticResource ParticleOriginAngleP4}"/> </TransformGroup> </Ellipse.RenderTransform> </Ellipse> </Border> </Canvas> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
在構建中發明,一開端在設定綁准時,寫成<SolidColorBrush x:Key = "ParticleColor" Color = "{Binding Path=FillColor}" />一向都沒法綁定勝利,後來查了材料,改成<SolidColorBrush x:Key = "ParticleColor" Color = "{Binding Path=FillColor,RelativeSource={RelativeSource TemplatedParent}}" /> 後勝利。
4、編纂Loading.cs文件,對自界說屬性FillColor和邏輯停止編碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfControlLibraryDemo { using System.ComponentModel; /// <summary> /// 依照步調 1a 或 1b 操作,然後履行步調 2 以在 XAML 文件中應用此自界說控件。 /// /// 步調 1a) 在以後項目中存在的 XAML 文件中應用該自界說控件。 /// 將此 XmlNamespace 特征添加到要應用該特征的標志文件的根 /// 元素中: /// /// xmlns:MyNamespace="clr-namespace:WpfControlLibraryDemo" /// /// /// 步調 1b) 在其他項目中存在的 XAML 文件中應用該自界說控件。 /// 將此 XmlNamespace 特征添加到要應用該特征的標志文件的根 /// 元素中: /// /// xmlns:MyNamespace="clr-namespace:WpfControlLibraryDemo;assembly=WpfControlLibraryDemo" /// /// 您還須要添加一個從 XAML 文件地點的項目到此項目標項目援用, /// 偏重重生成以免編譯毛病: /// /// 在處理計劃資本治理器中右擊目的項目,然後順次單擊 /// “添加援用”->“項目”->[閱讀查找並選擇此項目] /// /// /// 步調 2) /// 持續操作並在 XAML 文件中應用控件。 /// /// <MyNamespace:Loading/> /// /// </summary> public class Loading : Control { static Loading() { //重載默許款式 DefaultStyleKeyProperty.OverrideMetadata(typeof(Loading), new FrameworkPropertyMetadata(typeof(Loading))); //DependencyProperty 注冊 FillColor FillColorProperty = DependencyProperty.Register("FillColor", typeof(Color), typeof(Loading), new UIPropertyMetadata(Colors.DarkBlue, new PropertyChangedCallback(OnUriChanged)) ); //Colors.DarkBlue為控件初始化默許值 } //屬性變革回調函數 private static void OnUriChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { //Border b = (Border)d; //MessageBox.Show(e.NewValue.ToString()); } #region 自界說Fields // DependencyProperty屬性界說 FillColorProperty=FillColor+Property構成 public static readonly DependencyProperty FillColorProperty; #endregion //VS設計器屬性支撐 [Description("配景色"), Category("特性設置裝備擺設"), DefaultValue("#FF668899")] public Color FillColor { //GetValue,SetValue為固定寫法,此處普通不建議處置其他邏輯 get { return (Color)GetValue(FillColorProperty); } set { SetValue(FillColorProperty, value); } } } }
5、編譯,假如無誤後,可以添加WPF運用法式WpfAppLoadingTest停止測試(添加項目援用)。
翻開MainWindow.xaml,將Loading控件拖放到設計界面上,以下圖所示:
6、控件色彩修正,選中控件,在屬性欄中停止設置裝備擺設便可:
7.總結
可以看到WPF自界說控件照樣比擬輕易的,然則難點在於UI的設計,假如須要做的雅觀,須要美工的介入,並且須要轉換成XAML。
以上就是WPF完成炫酷Loading控件的全體內容,願望對年夜家的進修有所贊助。