当前位置:   article > 正文

Avalonia学习(二十九)-仪表

Avalonia学习(二十九)-仪表

Avalonia制作仪表盘,把控件给大家演示一下,Avalonia有三类自定义控件,分别是用户控件、模版控件、自主控件。前面已经很多用户控件了,这个是演示模版控件,另外一种不知道哪种情况下使用。

前端代码:

  1. <Styles xmlns="https://github.com/avaloniaui"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3. xmlns:local="clr-namespace:GaugeAvalonia.Views;assembly=GaugeAvalonia"
  4. x:CompileBindings="False"
  5. >
  6. <Design.PreviewWith>
  7. <Border Padding="20">
  8. <!-- Add Controls for Previewer Here -->
  9. </Border>
  10. </Design.PreviewWith>
  11. <Style Selector="local|ArcGauge">
  12. <Setter Property="Background" Value="#646464"/>
  13. <Setter Property="Foreground" Value="Black"/>
  14. <Setter Property="Template">
  15. <Setter.Value>
  16. <ControlTemplate TargetType="{x:Type local:ArcGauge}">
  17. <Border Margin="10">
  18. <Grid Width="{Binding RelativeSource={RelativeSource Self},Path=Height}">
  19. <Ellipse Fill="#FF3B3B3B"/>
  20. <Grid RenderTransformOrigin="0.5,0.5" Margin="2">
  21. <Grid.RenderTransform>
  22. <TransformGroup>
  23. <RotateTransform Angle="{Binding Angle}"/>
  24. </TransformGroup>
  25. </Grid.RenderTransform>
  26. <Ellipse Width="16" Height="14" Fill="Orange" VerticalAlignment="Top" >
  27. <Ellipse.Effect>
  28. <BlurEffect Radius="12"/>
  29. </Ellipse.Effect>
  30. </Ellipse>
  31. </Grid>
  32. <Grid x:Name="bdGrid" Margin="12" UseLayoutRounding="True" ClipToBounds="True">
  33. <Ellipse>
  34. <Ellipse.Fill>
  35. <RadialGradientBrush>
  36. <GradientStop Color="#4D000000"/>
  37. </RadialGradientBrush>
  38. </Ellipse.Fill>
  39. </Ellipse>
  40. <Grid>
  41. <Grid.ColumnDefinitions>
  42. <ColumnDefinition/>
  43. <ColumnDefinition Width="2*"/>
  44. <ColumnDefinition/>
  45. </Grid.ColumnDefinitions>
  46. <Grid.RowDefinitions>
  47. <RowDefinition/>
  48. <RowDefinition Height="2*"/>
  49. <RowDefinition/>
  50. </Grid.RowDefinitions>
  51. <Ellipse Stroke="#464646" StrokeThickness="1" Grid.Column="1" Grid.Row="1"/>
  52. <Ellipse Stroke="#959595" Margin="4" StrokeThickness="6" Grid.Column="1" Grid.Row="1"/>
  53. <Ellipse Stroke="#464646" Margin="14" StrokeThickness="1" Grid.Column="1" Grid.Row="1"/>
  54. </Grid>
  55. <Grid>
  56. <Grid.RowDefinitions>
  57. <RowDefinition/>
  58. <RowDefinition/>
  59. </Grid.RowDefinitions>
  60. <Path Data="M5,0 5,0 10,120 0,120z" Fill="#0FA9CE" Stretch="Uniform" Margin="0 30 0 0" HorizontalAlignment="Center">
  61. <Path.RenderTransform>
  62. <TransformGroup>
  63. <RotateTransform Angle="{Binding Path=Angle, Mode=TwoWay}"/>
  64. </TransformGroup>
  65. </Path.RenderTransform>
  66. </Path>
  67. </Grid>
  68. <Ellipse Width="28" Height="28" Fill="Black">
  69. <Ellipse.Effect>
  70. <!--<DropShadowEffect Color="#0FA9CE" ShadowDepth="0" Direction="0" BlurRadius="16"/>-->
  71. </Ellipse.Effect>
  72. </Ellipse>
  73. <Border VerticalAlignment="Bottom" BorderBrush="#10ABD1" BorderThickness="2" Margin="0 0 0 12" Background="Black" Padding="8 2" HorizontalAlignment="Center">
  74. <TextBlock Text="{Binding Value,RelativeSource={RelativeSource Mode=TemplatedParent}}" FontSize="16" Width="30" TextAlignment="Center" Foreground="White" FontWeight="Bold"/>
  75. </Border>
  76. </Grid>
  77. </Grid>
  78. </Border>
  79. </ControlTemplate>
  80. </Setter.Value>
  81. </Setter>
  82. </Style>
  83. </Styles>

后台代码:

  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.Shapes;
  4. using Avalonia.Media;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using Avalonia.Controls.Templates;
  8. using Avalonia.Controls.Primitives;
  9. using System.Linq;
  10. namespace GaugeAvalonia.Views
  11. {
  12. public class ArcGauge: TemplatedControl
  13. {
  14. Grid bdGrid;
  15. static ArcGauge()
  16. {
  17. // DefaultStyleKeyProperty.OverrideMetadata(typeof(ArcGauge), new FrameworkPropertyMetadata(typeof(ArcGauge)));
  18. }
  19. public ArcGauge()
  20. {
  21. this.Loaded += ArcGauge_Loaded;
  22. //Width = 200;
  23. //Height = 200;
  24. SetCurrentValue(ValueProperty, 0d);
  25. SetCurrentValue(MinValueProperty, 0d);
  26. SetCurrentValue(MaxValueProperty, 100d);
  27. }
  28. private void ArcGauge_Loaded(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
  29. {
  30. InitTick();
  31. }
  32. public override void Render(DrawingContext context)
  33. {
  34. base.Render(context);
  35. bdGrid = (Grid)this.GetTemplateChildren().Where(x => x.Name == "bdGrid").First();
  36. Refresh();
  37. }
  38. private void InitTick()
  39. {
  40. // 画大刻度
  41. for (int i = 0; i < 9; i++)
  42. {
  43. Line line = new Line();
  44. line.StartPoint = new Point(0, 0);
  45. line.EndPoint=new Point(0, 12);
  46. line.HorizontalAlignment= Avalonia.Layout.HorizontalAlignment
  47. .Center;
  48. line.Stroke = Brushes.White;
  49. line.StrokeThickness = 2;
  50. line.RenderTransformOrigin = RelativePoint.Center;
  51. line.RenderTransform = new RotateTransform() { Angle = -140 + i * 35 };
  52. bdGrid.Children.Add(line);
  53. DrawText();
  54. } // 画小刻度
  55. for (int i = 0; i < 8; i++)
  56. {
  57. var start = -140 + 35 * i + 3.5;
  58. for (int j = 0; j < 9; j++)
  59. {
  60. Line line = new Line();
  61. line.StartPoint = new Point(0, 0);
  62. line.EndPoint = new Point(0, 6);
  63. line.Stroke = Brushes.White;
  64. line.StrokeThickness = 1;
  65. line.HorizontalAlignment = Avalonia.Layout.HorizontalAlignment .Center;
  66. line.RenderTransformOrigin = RelativePoint.Center;
  67. line.RenderTransform = new RotateTransform() { Angle = start + j * 3.5 };
  68. bdGrid.Children.Add(line);
  69. }
  70. }
  71. }
  72. List<TextBlock> textLabels = new List<TextBlock>();
  73. private void DrawText()
  74. {
  75. foreach (var item in textLabels)
  76. {
  77. bdGrid.Children.Remove(item);
  78. }
  79. textLabels.Clear();
  80. var per = MaxValue / 8;
  81. for (int i = 0; i < 9; i++)
  82. {
  83. TextBlock textBlock = new TextBlock();
  84. textBlock.Text = $"{MinValue + (per * i)}";
  85. textBlock.HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center;
  86. textBlock.RenderTransformOrigin = RelativePoint.Center;
  87. textBlock.RenderTransform = new RotateTransform() { Angle = -140 + i * 35 };
  88. textBlock.Margin = new Thickness(12);
  89. textBlock.Foreground = Brushes.White;
  90. bdGrid.Children.Add(textBlock);
  91. textLabels.Add(textBlock);
  92. }
  93. }
  94. //public static readonly StyledProperty<IBrush> BackgroundProperty =
  95. // AvaloniaProperty.Register<ArcGauge, IBrush>(nameof(Value));
  96. //public IBrush Background
  97. //{
  98. // get
  99. // {
  100. // return GetValue(BackgroundProperty);
  101. // }
  102. // set
  103. // {
  104. // SetValue(BackgroundProperty, value);
  105. // }
  106. //}
  107. //public static readonly StyledProperty<IBrush> ForegroundProperty =
  108. // AvaloniaProperty.Register<ArcGauge, IBrush>(nameof(Value));
  109. //public IBrush Foreground
  110. //{
  111. // get
  112. // {
  113. // return GetValue(ForegroundProperty);
  114. // }
  115. // set
  116. // {
  117. // SetValue(ForegroundProperty, value);
  118. // }
  119. // }
  120. [Category("值设定")]
  121. public double Value
  122. {
  123. get { return (double)GetValue(ValueProperty); }
  124. set { SetValue(ValueProperty, value); }
  125. }
  126. public static readonly StyledProperty<double> ValueProperty =
  127. AvaloniaProperty.Register<ArcGauge, double>(nameof(Value), coerce: OnValueChanged);
  128. private static double OnValueChanged(AvaloniaObject @object, double arg2)
  129. {
  130. ArcGauge gauge= @object as ArcGauge;
  131. gauge.Refresh();
  132. return arg2;
  133. }
  134. [Category("值设定")]
  135. public double MinValue
  136. {
  137. get { return (double)GetValue(MinValueProperty); }
  138. set { SetValue(MinValueProperty, value); }
  139. }
  140. public static readonly StyledProperty<double> MinValueProperty =
  141. AvaloniaProperty.Register<ArcGauge, double>(nameof(MinValue), coerce: OnValueChanged);
  142. public double MaxValue
  143. {
  144. get { return (double)GetValue(MaxValueProperty); }
  145. set { SetValue(MaxValueProperty, value); }
  146. }
  147. public static readonly StyledProperty<double> MaxValueProperty =
  148. AvaloniaProperty.Register<ArcGauge, double>(nameof(MaxValue), coerce: OnValueChanged);
  149. public double Angle
  150. {
  151. get { return (double)GetValue(AngleProperty); }
  152. set { SetValue(AngleProperty, value); }
  153. }
  154. public static readonly StyledProperty<double> AngleProperty =
  155. AvaloniaProperty.Register<ArcGauge, double>(nameof(Angle));
  156. private void Refresh()
  157. {
  158. if (Value > MaxValue)
  159. {
  160. Angle = 140;
  161. }
  162. else if (Value < MinValue)
  163. {
  164. Angle = -140;
  165. }
  166. else
  167. {
  168. var range = MaxValue - MinValue;
  169. var process = Value / range;
  170. var tAngle = process * 280 - 140;
  171. Angle = tAngle;
  172. }
  173. }
  174. }
  175. }

运行效果:

目前Avalonia的内容也不知道该演示什么了。估计博文会更新慢了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/188478?site
推荐阅读
相关标签
  

闽ICP备14008679号