Guest User

Untitled

a guest
Jun 25th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1. Animation.Duration = new Duration(TimeSpan.FromSeconds(Seconds));
  2.  
  3. public class Arc : Shape
  4. {
  5. public Point Center
  6. {
  7. get => (Point)GetValue(CenterProperty);
  8. set => SetValue(CenterProperty, value);
  9. }
  10.  
  11. // Using a DependencyProperty as the backing store for Center. This enables animation, styling, binding, etc...
  12. public static readonly DependencyProperty CenterProperty =
  13. DependencyProperty.Register("Center", typeof(Point), typeof(Arc),
  14. new FrameworkPropertyMetadata(new Point(), FrameworkPropertyMetadataOptions.AffectsRender));
  15.  
  16. // Start angle in degrees
  17. public double StartAngle
  18. {
  19. get => (double)GetValue(StartAngleProperty);
  20. set => SetValue(StartAngleProperty, value);
  21. }
  22.  
  23. // Using a DependencyProperty as the backing store for StartAngle. This enables animation, styling, binding, etc...
  24. public static readonly DependencyProperty StartAngleProperty =
  25. DependencyProperty.Register("StartAngle", typeof(double), typeof(Arc),
  26. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  27.  
  28. // End angle in degrees
  29. public double EndAngle
  30. {
  31. get => (double)GetValue(EndAngleProperty);
  32. set => SetValue(EndAngleProperty, value);
  33. }
  34.  
  35. // Using a DependencyProperty as the backing store for EndAngle. This enables animation, styling, binding, etc...
  36. public static readonly DependencyProperty EndAngleProperty =
  37. DependencyProperty.Register("EndAngle", typeof(double), typeof(Arc),
  38. new FrameworkPropertyMetadata(90.0, FrameworkPropertyMetadataOptions.AffectsRender));
  39.  
  40. public double Radius
  41. {
  42. get => (double)GetValue(RadiusProperty);
  43. set => SetValue(RadiusProperty, value);
  44. }
  45.  
  46. // Using a DependencyProperty as the backing store for Radius. This enables animation, styling, binding, etc...
  47. public static readonly DependencyProperty RadiusProperty =
  48. DependencyProperty.Register("Radius", typeof(double), typeof(Arc),
  49. new FrameworkPropertyMetadata(10.0, FrameworkPropertyMetadataOptions.AffectsRender));
  50.  
  51. public bool SmallAngle
  52. {
  53. get => (bool)GetValue(SmallAngleProperty);
  54. set => SetValue(SmallAngleProperty, value);
  55. }
  56.  
  57. // Using a DependencyProperty as the backing store for SmallAngle. This enables animation, styling, binding, etc...
  58. public static readonly DependencyProperty SmallAngleProperty =
  59. DependencyProperty.Register("SmallAngle", typeof(bool), typeof(Arc),
  60. new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
  61.  
  62. static Arc() => DefaultStyleKeyProperty.OverrideMetadata(typeof(Arc), new FrameworkPropertyMetadata(typeof(Arc)));
  63.  
  64. protected override Geometry DefiningGeometry
  65. {
  66. get
  67. {
  68. double startAngleRadians = StartAngle * Math.PI / 180;
  69. double endAngleRadians = EndAngle * Math.PI / 180;
  70.  
  71. double a0 = StartAngle < 0 ? startAngleRadians + 2 * Math.PI : startAngleRadians;
  72. double a1 = EndAngle < 0 ? endAngleRadians + 2 * Math.PI : endAngleRadians;
  73.  
  74. if (a1 < a0)
  75. a1 += Math.PI * 2;
  76.  
  77. SweepDirection d = SweepDirection.Counterclockwise;
  78. bool large;
  79.  
  80. if (SmallAngle)
  81. {
  82. large = false;
  83. double t = a1;
  84. d = (a1 - a0) > Math.PI ? SweepDirection.Counterclockwise : SweepDirection.Clockwise;
  85. }
  86. else
  87. large = (Math.Abs(a1 - a0) < Math.PI);
  88.  
  89. Point p0 = Center + new Vector(Math.Cos(a0), Math.Sin(a0)) * Radius;
  90. Point p1 = Center + new Vector(Math.Cos(a1), Math.Sin(a1)) * Radius;
  91.  
  92. List<PathSegment> segments = new List<PathSegment>
  93. {
  94. new ArcSegment(p1, new Size(Radius, Radius), 0.0, large, d, true)
  95. };
  96.  
  97. List<PathFigure> figures = new List<PathFigure>
  98. {
  99. new PathFigure(p0, segments, true)
  100. {
  101. IsClosed = false
  102. }
  103. };
  104.  
  105. return new PathGeometry(figures, FillRule.EvenOdd, null);
  106. }
  107. }
  108. }
  109.  
  110. <UserControl x:Class="WpfApp3.Countdown"
  111. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  112. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  113. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  114. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  115. xmlns:local="clr-namespace:WpfApp"
  116. mc:Ignorable="d"
  117. d:DesignHeight="450" d:DesignWidth="450" Loaded="UserControl_Loaded">
  118. <UserControl.Triggers>
  119. <EventTrigger RoutedEvent="UserControl.Loaded">
  120. <BeginStoryboard>
  121. <Storyboard>
  122. <DoubleAnimation Name="Animation"
  123. Storyboard.TargetName="Arc"
  124. Storyboard.TargetProperty="EndAngle"
  125. From="-90"
  126. To="270" />
  127. </Storyboard>
  128. </BeginStoryboard>
  129. </EventTrigger>
  130. </UserControl.Triggers>
  131. <Viewbox>
  132. <Grid Width="100" Height="100">
  133. <Border Background="#222" Margin="5" CornerRadius="50">
  134. <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
  135. <Label Foreground="#fff" Content="{Binding Seconds}" FontSize="50" Margin="0, -10, 0, 0" />
  136. <Label Foreground="#fff" Content="sec" HorizontalAlignment="Center" Margin="0, -20, 0, 0" />
  137. </StackPanel>
  138. </Border>
  139.  
  140. <local:Arc
  141. x:Name="Arc"
  142. Center="50, 50"
  143. StartAngle="-90"
  144. EndAngle="-90"
  145. Stroke="#45d3be"
  146. StrokeThickness="5"
  147. Radius="45" />
  148. </Grid>
  149. </Viewbox>
  150. </UserControl>
  151.  
  152. public partial class Countdown : UserControl
  153. {
  154. public int Seconds
  155. {
  156. get => (int)GetValue(SecondsProperty);
  157. set => SetValue(SecondsProperty, value);
  158. }
  159.  
  160. public static readonly DependencyProperty SecondsProperty =
  161. DependencyProperty.Register(nameof(Seconds), typeof(int), typeof(Countdown), new PropertyMetadata(0));
  162.  
  163. private readonly DispatcherTimer _timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
  164.  
  165. public Countdown()
  166. {
  167. InitializeComponent();
  168. DataContext = this;
  169. }
  170.  
  171. private void UserControl_Loaded(object sender, EventArgs e)
  172. {
  173. Animation.Duration = new Duration(TimeSpan.FromSeconds(Seconds));
  174. if (Seconds > 0)
  175. {
  176. _timer.Start();
  177. _timer.Tick += Timer_Tick;
  178. }
  179. }
  180.  
  181. private void Timer_Tick(object sender, EventArgs e)
  182. {
  183. Seconds--;
  184. if (Seconds == 0) _timer.Stop();
  185. }
  186. }
  187.  
  188. <local:Countdown Width="300" Height="300" Seconds="25" />
  189.  
  190. public partial class Countdown : UserControl
  191. {
  192. public int Seconds
  193. {
  194. get => (int)GetValue(SecondsProperty);
  195. set => SetValue(SecondsProperty, value);
  196. }
  197.  
  198. public static readonly DependencyProperty SecondsProperty =
  199. DependencyProperty.Register(nameof(Seconds), typeof(int), typeof(Countdown), new PropertyMetadata(0));
  200.  
  201. public Countdown()
  202. {
  203. InitializeComponent();
  204. DataContext = this;
  205. }
  206.  
  207. private async void UserControl_Loaded(object sender, EventArgs e)
  208. {
  209. Animation.Duration = new Duration(TimeSpan.FromSeconds(Seconds));
  210.  
  211. while(Seconds > 0)
  212. {
  213. var delayTask = Task.Delay(1000);
  214. Seconds--;
  215. await delayTask;
  216. }
  217. }
  218. }
Add Comment
Please, Sign In to add comment