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库和主界面,很多代码就不贴代码了,完善的效果展示一下。

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

相关推荐
武子康6 小时前
Java-82 深入浅出 MySQL 内部架构:服务层、存储引擎与文件系统全覆盖
java·开发语言·数据库·学习·mysql·spring·微服务
霜绛9 小时前
机器学习笔记(三)——决策树、随机森林
人工智能·笔记·学习·决策树·随机森林·机器学习
吗喽对你问好10 小时前
Android UI 控件详解实践
android·ui
站住前面的二哈10 小时前
Cartographer安装测试与模块开发(三)--Cartographer在Gazebo仿真环境下的建图以及建图与定位阶段问题(实车也可参考)
学习·ubuntu
★YUI★11 小时前
学习游戏制作记录(克隆技能)7.25
学习·游戏·unity·c#
屁股割了还要学11 小时前
【C语言进阶】柔性数组
c语言·开发语言·数据结构·c++·学习·算法·柔性数组
woodykissme12 小时前
UG创建的实体橘黄色实体怎么改颜色?
学习·齿轮·ug建模
Feather_7413 小时前
从Taro的Dialog.open出发,学习远程控制组件之【事件驱动】
javascript·学习·taro
星仔编程13 小时前
python学习DAY22打卡
学习
波波鱼દ ᵕ̈ ૩14 小时前
学习:JS[6]环境对象+回调函数+事件流+事件委托+其他事件+元素尺寸位置
前端·javascript·学习