Avalonia學習(二十九)-儀表

Avalonia制作儀表盤,把控件給大家演示一下,Avalonia有三類自定義控件,分別是用戶控件、模版控件、自主控件。前面已經很多用戶控件了,這個是演示模版控件,另外一種不知道哪種情況下使用。

前端代碼:

<Styles xmlns="https://github.com/avaloniaui"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:GaugeAvalonia.Views;assembly=GaugeAvalonia"x:CompileBindings="False"><Design.PreviewWith><Border Padding="20"><!-- Add Controls for Previewer Here --></Border></Design.PreviewWith><Style  Selector="local|ArcGauge"><Setter Property="Background" Value="#646464"/><Setter Property="Foreground" Value="Black"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type local:ArcGauge}"><Border Margin="10"><Grid Width="{Binding RelativeSource={RelativeSource Self},Path=Height}"><Ellipse Fill="#FF3B3B3B"/><Grid RenderTransformOrigin="0.5,0.5" Margin="2"><Grid.RenderTransform><TransformGroup><RotateTransform  Angle="{Binding Angle}"/></TransformGroup></Grid.RenderTransform><Ellipse Width="16" Height="14" Fill="Orange" VerticalAlignment="Top" ><Ellipse.Effect><BlurEffect Radius="12"/></Ellipse.Effect></Ellipse></Grid><Grid x:Name="bdGrid" Margin="12" UseLayoutRounding="True" ClipToBounds="True"><Ellipse><Ellipse.Fill><RadialGradientBrush><GradientStop Color="#4D000000"/></RadialGradientBrush></Ellipse.Fill></Ellipse><Grid><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition Width="2*"/><ColumnDefinition/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition/><RowDefinition Height="2*"/><RowDefinition/></Grid.RowDefinitions><Ellipse Stroke="#464646" StrokeThickness="1" Grid.Column="1" Grid.Row="1"/><Ellipse Stroke="#959595" Margin="4" StrokeThickness="6" Grid.Column="1" Grid.Row="1"/><Ellipse Stroke="#464646" Margin="14" StrokeThickness="1" Grid.Column="1" Grid.Row="1"/></Grid><Grid><Grid.RowDefinitions><RowDefinition/><RowDefinition/></Grid.RowDefinitions><Path  Data="M5,0 5,0 10,120 0,120z" Fill="#0FA9CE" Stretch="Uniform" Margin="0 30 0 0"  HorizontalAlignment="Center"><Path.RenderTransform><TransformGroup><RotateTransform    Angle="{Binding Path=Angle, Mode=TwoWay}"/></TransformGroup></Path.RenderTransform></Path></Grid><Ellipse Width="28" Height="28" Fill="Black"><Ellipse.Effect><!--<DropShadowEffect Color="#0FA9CE" ShadowDepth="0" Direction="0" BlurRadius="16"/>--></Ellipse.Effect></Ellipse><Border VerticalAlignment="Bottom" BorderBrush="#10ABD1" BorderThickness="2" Margin="0 0 0 12" Background="Black" Padding="8 2" HorizontalAlignment="Center"><TextBlock Text="{Binding Value,RelativeSource={RelativeSource Mode=TemplatedParent}}" FontSize="16" Width="30" TextAlignment="Center" Foreground="White" FontWeight="Bold"/></Border></Grid></Grid></Border></ControlTemplate></Setter.Value></Setter></Style></Styles>

后臺代碼:

using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Media;
using System.Collections.Generic;
using System.ComponentModel;
using Avalonia.Controls.Templates;
using Avalonia.Controls.Primitives;
using System.Linq;namespace GaugeAvalonia.Views
{public class ArcGauge: TemplatedControl{Grid bdGrid;static ArcGauge(){// DefaultStyleKeyProperty.OverrideMetadata(typeof(ArcGauge), new FrameworkPropertyMetadata(typeof(ArcGauge)));}public ArcGauge(){this.Loaded += ArcGauge_Loaded;//Width = 200;//Height = 200;SetCurrentValue(ValueProperty, 0d);SetCurrentValue(MinValueProperty, 0d);SetCurrentValue(MaxValueProperty, 100d);}private void ArcGauge_Loaded(object? sender, Avalonia.Interactivity.RoutedEventArgs e){InitTick();}public override void Render(DrawingContext context){base.Render(context);bdGrid = (Grid)this.GetTemplateChildren().Where(x => x.Name == "bdGrid").First();Refresh();}private void InitTick(){// 畫大刻度for (int i = 0; i < 9; i++){Line line = new Line();line.StartPoint = new Point(0, 0);line.EndPoint=new Point(0, 12);line.HorizontalAlignment= Avalonia.Layout.HorizontalAlignment.Center;line.Stroke = Brushes.White;line.StrokeThickness = 2;line.RenderTransformOrigin = RelativePoint.Center;line.RenderTransform = new RotateTransform() { Angle = -140 + i * 35 };bdGrid.Children.Add(line);DrawText();}            // 畫小刻度for (int i = 0; i < 8; i++){var start = -140 + 35 * i + 3.5;for (int j = 0; j < 9; j++){Line line = new Line();line.StartPoint = new Point(0, 0);line.EndPoint = new Point(0, 6);line.Stroke = Brushes.White;line.StrokeThickness = 1;line.HorizontalAlignment = Avalonia.Layout.HorizontalAlignment .Center;line.RenderTransformOrigin = RelativePoint.Center;line.RenderTransform = new RotateTransform() { Angle = start + j * 3.5 };bdGrid.Children.Add(line);}}}List<TextBlock> textLabels = new List<TextBlock>();private void DrawText(){foreach (var item in textLabels){bdGrid.Children.Remove(item);}textLabels.Clear();var per = MaxValue / 8;for (int i = 0; i < 9; i++){TextBlock textBlock = new TextBlock();textBlock.Text = $"{MinValue + (per * i)}";textBlock.HorizontalAlignment =  Avalonia.Layout.HorizontalAlignment.Center;textBlock.RenderTransformOrigin = RelativePoint.Center;textBlock.RenderTransform = new RotateTransform() { Angle = -140 + i * 35 };textBlock.Margin = new Thickness(12);textBlock.Foreground = Brushes.White;bdGrid.Children.Add(textBlock);textLabels.Add(textBlock);}}//public static readonly StyledProperty<IBrush> BackgroundProperty =//   AvaloniaProperty.Register<ArcGauge, IBrush>(nameof(Value));//public IBrush Background//{//    get//    {//        return GetValue(BackgroundProperty);//    }//    set//    {//        SetValue(BackgroundProperty, value);//    }//}//public static readonly StyledProperty<IBrush> ForegroundProperty =// AvaloniaProperty.Register<ArcGauge, IBrush>(nameof(Value));//public IBrush Foreground//{//    get//    {//        return GetValue(ForegroundProperty);//    }//    set//    {//        SetValue(ForegroundProperty, value);//    }// }[Category("值設定")]public double Value{get { return (double)GetValue(ValueProperty); }set { SetValue(ValueProperty, value); }}public static readonly StyledProperty<double> ValueProperty =AvaloniaProperty.Register<ArcGauge, double>(nameof(Value), coerce: OnValueChanged);private static double OnValueChanged(AvaloniaObject @object, double arg2){ArcGauge gauge= @object  as ArcGauge;gauge.Refresh();return arg2;}[Category("值設定")]public double MinValue{get { return (double)GetValue(MinValueProperty); }set { SetValue(MinValueProperty, value); }}public static readonly StyledProperty<double> MinValueProperty =AvaloniaProperty.Register<ArcGauge, double>(nameof(MinValue), coerce: OnValueChanged);public double MaxValue{get { return (double)GetValue(MaxValueProperty); }set { SetValue(MaxValueProperty, value); }}public static readonly StyledProperty<double> MaxValueProperty =AvaloniaProperty.Register<ArcGauge, double>(nameof(MaxValue), coerce: OnValueChanged);public double Angle{get { return (double)GetValue(AngleProperty); }set { SetValue(AngleProperty, value); }}public static readonly StyledProperty<double> AngleProperty =AvaloniaProperty.Register<ArcGauge, double>(nameof(Angle));private void Refresh(){if (Value > MaxValue){Angle = 140;}else if (Value < MinValue){Angle = -140;}else{var range = MaxValue - MinValue;var process = Value / range;var tAngle = process * 280 - 140;Angle = tAngle;}}}
}

運行效果:

目前Avalonia的內容也不知道該演示什么了。估計博文會更新慢了。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/717307.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/717307.shtml
英文地址,請注明出處:http://en.pswp.cn/news/717307.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

想從事數據方向職場小白看過來, 數據方面的一些英文解釋

想從事數據方向職場小白看過來&#xff0c;一些英文名詞解釋 文章目錄 想從事數據方向職場小白看過來&#xff0c;一些英文名詞解釋 英文類解釋NoSQL&#xff1a;ESB&#xff1a;ACID &#xff1a;Data Vault&#xff1a;MDM&#xff1a;OLAP&#xff1a;SCD:SBA&#xff1a;MP…

【Django】執行查詢——比較、刪除、復制、批量修改對象

以下述模型為基礎&#xff0c;討論檢索對象的方式方法&#xff1a; from datetime import datefrom django.db import modelsclass Blog(models.Model):name models.CharField(max_length100)tagline models.TextField()def __str__(self):return self.nameclass Author(mod…

【vue】v-if、v-show、v-for 相關所有面試題總結

v-if 和 v-show 的區別 兩個重點【dom】和【生命周期】 v-if 惰性指令&#xff0c;false 不會被編譯、渲染不會存在 DOM 中切換開銷大&#xff0c;需要重新創建元素值變化&#xff0c;使用 v-if 的組件生命周期執行順序 true 變為 false【組件的銷毀】 beforeDestroy / befor…

[Flutter]shared_preferences基本用法以及可視化管理存儲的key和value類型

shared_preferences 是一個Flutter插件&#xff0c;它提供了一種簡單的方式來在應用程序中存儲和獲取持久化的鍵值對數據。它可以用于存儲應用程序的配置信息、用戶偏好設置、登錄狀態等。 使用 shared_preferences 插件&#xff0c;你可以在應用程序中輕松地保存和讀取數據&a…

Java中線程相關的知識

創建子線程的三種方式: 1.自定義線程任務類繼承線程類&#xff0c;以便繼承其功能,重寫其run方法(里面寫自己需要實現的功能)&#xff0c;在main方法調用時創建其任務類實例化對象&#xff0c;然后調用對象的start方法(繼承自父類)&#xff0c;即成功創建線程 優點:創建方式簡…

Pandas DataFrame 基本操作實例100個

Pandas 是一個基于NumPy的數據分析模塊&#xff0c;最初由AQR Capital Management于2008年4月開發&#xff0c;并于2009年底開源。Pandas的名稱來源于“Panel Data”&#xff08;面板數據&#xff09;和“Python數據分析”&#xff08;data analysis&#xff09;。這個庫現在由…

來不及了!大學必須完成的四件事!

老師們常說&#xff0c;上大學就輕松了 其實不然 大學不是人生的終點&#xff0c;而是新的起跑線 不是休息站&#xff0c;而是進入社會的最后沖刺跑道 大學生活苦樂參半&#xff0c;成人世界即將來臨 出了校門&#xff0c;你會發現社會復雜多變&#xff0c;需要不斷學習 稍…

excel中如何使用VLOOKUP和EXACT函數實現區分大小寫匹配數據

在 Excel 中&#xff0c;VLOOKUP 函數默認情況下是不區分大小寫的&#xff1a; 比如下面的案例&#xff0c;直接使用VLOOKUP函數搜索&#xff0c;只會搜索匹配到不區分大小寫的第一個 如果我們想要實現區分大小寫的精確匹配&#xff0c;可以使用 EXACT 函數結合 VLOOKUP 函數 …

【簡說八股】Redisson的守護線程是怎么實現的

Redisson Redisson 是一個 Java 語言實現的 Redis SDK 客戶端&#xff0c;在使用分布式鎖時&#xff0c;它就采用了「自動續期」的方案來避免鎖過期&#xff0c;這個守護線程我們一般也把它叫做「看門狗」線程。 Redission是一個在Java環境中使用的開源的分布式緩存和分布式鎖實…

PyTorch-卷積神經網絡

卷積神經網絡 基本結構 首先解釋一下什么是卷積&#xff0c;這個卷積當然不是數學上的卷積&#xff0c;這里的卷積其實表示的是一個三維的權重&#xff0c;這么解釋起來可能不太理解&#xff0c;我們先看看卷積網絡的基本結構。 通過上面的圖我們清楚地了解到卷積網絡和一般網…

【Javascript】設計模式之發布訂閱模式

文章目錄 1、現實中的發布&#xff0d;訂閱模式2、DOM 事件3、簡單的發布-訂閱模式4、通用的發布-訂閱模式5、先發布再訂閱6、小結 發布—訂閱模式又叫觀察者模式&#xff0c;它定義對象間的一種一對多的依賴關系&#xff0c;當一個對象的狀態發生改變時&#xff0c;所有依賴于…

Mysql深入學習 基礎篇 Ss.02 詳解四類SQL語句

我親愛的對手&#xff0c;亦敵亦友&#xff0c;但我同樣希望你能成功&#xff0c;與我一起&#xff0c;站在人生的山頂上 ——24.3.1 一、DDL 數據定義語言 1.DDL —— 數據庫操作 查詢 查詢所有數據庫 show databases; 查詢當前數據庫 select database(); 創建 create databa…

【簡說八股】Nginx、GateWay、Ribbon有什么區別?

前言 在現代的微服務架構中&#xff0c;Nginx、Gateway 和 Ribbon 都是處理網絡請求和服務的組件&#xff0c;但它們各自扮演的角色和提供的功能有所不同。下面我將詳細解釋它們之間的區別&#xff1a; Nginx Nginx 是一個高性能的 HTTP 和反向代理服務器&#xff0c;它也可…

Golang Vs Java:為您的下一個項目選擇正確的工具

Java 首次出現在 1995 年&#xff0c;由 James Gosling 和 Sun Microsystems 的其他人開發的一種新編程語言。從那時起&#xff0c;Java 已成為世界上最受歡迎和廣泛使用的編程語言之一。Java 的主要特點包括其面向對象的設計、健壯性、平臺獨立性、自動內存管理以及廣泛的內置…

MSMFN

CDFI是彩色多普勒血流成像 輔助信息 作者未提供數據

Codeforces Round 930 (Div. 2)

substr時間復雜度O&#xff08;N&#xff09;&#xff0c;不能一遍遍找&#xff0c;會超時 #include<iostream> #include<algorithm> #include<vector> #include<map> using namespace std; const int N5e510; map<string,int>mp; vector<…

[C++]AVL樹怎么轉

AVL樹是啥 一提到AVL樹&#xff0c;腦子里不是旋了&#xff0c;就是懸了。 AVL樹之所以難&#xff0c;并不是因為結構難以理解&#xff0c;而是因為他的旋轉。 AVL樹定義 平衡因子&#xff1a;對于一顆二叉樹&#xff0c;某節點的左右子樹高度之差&#xff0c;就是該節點的…

5、云原生安全之falco的規則解讀(部分)(上)

文章目錄 1、自定義規則測試1.1、自定義檢測定時任務的規則2、自帶規則詳解部分2.1、意外的出站連接源(類似的還有入站連接)2.2、檢測目錄穿越攻擊2.3、rpm數據庫被修改2.4、數據庫派生新的進程2.5、特權容器啟動2.6、啟動容器掛載到敏感路徑2.7、匹配所有在pod內啟動、并連接…

音視頻數字化(數字與模擬-照相機)

目錄 1、模擬/數字 2、第一臺照相機 3、照相機原理 4、取景方式 5、底片 6、數碼相機 7、數碼相機指標 8、數碼相機分類 (1)單反相機 (2)單電相機 (3)無反相機

2024.03.02藍橋云課筆記

1.scanf與printf取消分隔符的限制方法 示例代碼&#xff1a; int main() { char s[10];scanf("%d[^\n]",s);printf("%s",s);return 0; } 運行&#xff1a; 輸入&#xff1a;Hello World 輸出&#xff1a;Hello World 注&#xff1a;其中[]中是一個正則…