Avalonia学习(二十三)-大屏

弄一个大屏显示的界面例子,但是代码有点多,还有用户控件。

目前还有一点问题在解决,先看一下界面效果。

圆形控件

前端代码

XML 复制代码
<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="ProAvalonia.Controls.CircularProgressBar">
  <Grid Name="layout" >
    <Ellipse StrokeThickness="8"  Name="backEllipse"
                 />
    <Path Data="M75.001 5 A70 70 0 1 1 75 5" StrokeThickness="6" Stroke="#CC2BB6FE"
          Name="path"/>

    <Viewbox Margin="14">
      <TextBlock 
                 VerticalAlignment="Center" HorizontalAlignment="Center"/>
    </Viewbox>

    <Grid VerticalAlignment="Top" Margin="0,-3,0,0">
      <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
      </Grid.ColumnDefinitions>
      <Canvas>
        <TextBlock 
                   HorizontalAlignment="Right" FontSize="9" Foreground="#55FFFFFF"
                   Canvas.Right="8"/>
      </Canvas>
    </Grid>
  </Grid>
</UserControl>

后台代码

cs 复制代码
using Avalonia.Controls;
using System.ComponentModel;
using System;
using Avalonia;
using System.Drawing;
using Avalonia.Media;
using Brush = System.Drawing.Brush;
using Color = System.Drawing.Color;

namespace ProAvalonia.Controls
{
    public partial class CircularProgressBar : UserControl
    {
        public static readonly StyledProperty<double> ValueProperty =
            AvaloniaProperty.Register<CircularProgressBar,double>(nameof(Value),0);
        public double Value
        {
            get { return (double)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        
        }
        //private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        //{
        //    (d as CircularProgressBar).UpdateValue();
        //}

        public static readonly StyledProperty<SolidColorBrush> BackColorProperty =
           AvaloniaProperty.Register<CircularProgressBar, SolidColorBrush>(nameof(BackColor));
        public SolidColorBrush BackColor
        {
            get { return (SolidColorBrush)GetValue(BackColorProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }
        public static readonly StyledProperty<string> TitleProperty =
          AvaloniaProperty.Register<CircularProgressBar, string>(nameof(Title));

        public CircularProgressBar()
        {
            InitializeComponent();
            this.SizeChanged += CircularProgressBar_SizeChanged;
        }

      
        private void CircularProgressBar_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            UpdateValue();
        }

        private void UpdateValue()
        {
            this.layout.Width = Math.Min(this.Width, this.Height);
            double radius = this.layout.Width / 2;
            if (radius == 0 || Value == 0) return;

            double newX = 0.0, newY = 0.0;
            newX = radius + (radius - 3) * Math.Cos((Value % 100 * 100 * 3.6 - 90) * Math.PI / 180);
            newY = radius + (radius - 3) * Math.Sin((Value % 100 * 100 * 3.6 - 90) * Math.PI / 180);

            string pathStr = $"M{radius + 0.01} 3 " +
                $"A{radius - 3} {radius - 3} 0 {(this.Value < 0.5 ? 0 : 1)} 1 {newX} {newY}";

            var converter = TypeDescriptor.GetConverter(typeof(Geometry));
            this.path.Data = (Geometry)converter.ConvertFrom(pathStr);
        }
    }
}

另外还有GroupBox库和主界面,很多代码就不贴代码了,完善的效果展示一下。

下一篇给大家展示一个物联的演示界面,请期待。

相关推荐
小浪学编程2 小时前
C#学习12——预处理
学习
Timmer丿2 小时前
kafka学习笔记(三、消费者Consumer使用教程——配置参数大全及性能调优)
笔记·学习·kafka
Timmer丿2 小时前
kafka学习笔记(三、消费者Consumer使用教程——消费性能多线程提升思考)
笔记·学习·kafka
战族狼魂2 小时前
转战web3远程工作的英语学习的路线规划
学习
颜妮儿3 小时前
地震资料裂缝定量识别——学习计划
学习
@蓝莓果粒茶3 小时前
LeetCode第244题_最短单词距离II
c++·笔记·学习·算法·leetcode·职场和发展·c#
肥肠可耐的西西公主3 小时前
前端(vue)学习笔记(CLASS 7):vuex
前端·笔记·学习
海尔辛3 小时前
Unity UI 性能优化终极指南 — Image篇
ui·unity·性能优化
ue星空4 小时前
UE音频中间件wwise插件
学习·ue5·音视频
越轨4 小时前
【Pytorch学习笔记】模型模块08——AlexNet模型详解
人工智能·pytorch·笔记·深度学习·学习·机器学习