
- using HTJ.Models;
- using HTJ.Views;
- using HTJ.Arx.Service.Interface;
- using Timer = System.Threading.Timer;
- namespace HTJ.ViewModels;
- public partial class TestViewModel : ObservableObject
- {
- private static readonly IUserSetting IuserSetting = AppX.IuserSetting;
- [ObservableProperty] private TestModel _vmData = IuserSetting.GetService<TestModel>();
- private bool increasing;
-
- private double curRadius;
-
- private double ang;
-
- private Timer _timer;
-
- [RelayCommand]
- private void Sel()
- {
- var view = AppHelper.GetService<TestView>();
- view.Hide();
- using var tr = new DBTrans(docLock: true);
- if (!Env.Editor.SelEnt(out DBText text, true, "请选择需要搜素的文字"))
- return;
- VmData.Name = text.TextString;
- view.ShowDialog();
- }
- [RelayCommand]
- private void Save()
- {
- IuserSetting.Save(VmData);
- }
- [RelayCommand]
- private void Read()
- {
- IuserSetting.ReLoad(VmData);
- }
- [RelayCommand]
- private void Add()
- {
- VmData.Value += 5;
- VmData.Name = $"Name{VmData.Value}";
- }
- [RelayCommand]
- private void Open()
- {
- try
- {
- Process.Start("explorer.exe", AppX.AppPath);
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- }
- }
- [RelayCommand]
- private void Stop()
- {
- var view = AppHelper.GetService<TestView>();
- view.Hide();
- Env.Document.SetFocus();
- _timer.Dispose();
- view.ShowDialog();
-
- }
- [RelayCommand]
- private void Render()
- {
- // 创建TimerCallback委托
- TimerCallback callback = TimerTask;
- // 创建一个状态对象,可以传递任何需要的数据
- var state = new TimerState
- {
- Message = "定时器已触发",
- Count = 0
- };
- // 设置初始延迟2秒,之后每隔1秒触发一次
- TimeSpan dueTime = TimeSpan.FromSeconds(2);
- TimeSpan period = TimeSpan.FromSeconds(0.01);
- // 创建计时器
- _timer = new Timer(callback, state, dueTime, period);
-
- }
- // 计时器任务方法
- void TimerTask(object state)
- {
- if (increasing)
- {
- curRadius+=1;
- if (curRadius>=200)
- increasing = false;
- }
- else
- {
- curRadius-=1;
- if (curRadius<=20)
- increasing = true;
- }
- VmData.Radius=curRadius;
- VmData.Thick=15-curRadius/15;
- // var ss=TimeSpan.FromSeconds(0.01);
- ang+=1;
- if (ang>360)
- ang %= 360;
- VmData.Ang = ang;
- var rad = ang.ToRadian();
- var x=150+100*Math.Cos(rad);
- var y=150+100*Math.Sin(rad);
-
- VmData.X = x;
- VmData.Y = y;
- // var timerState = (TimerState)state;
- // // 模拟一些工作
- // Thread.Sleep(300); // 300毫秒
- }
-
- // 状态类,用于传递数据给计时器回调
- public class TimerState
- {
- public string? Message { get; set; }
- public int Count { get; set; }
- }
-
- }

- using System.Windows.Media;
- using HTJ.Arx.Service.Interface;
- using Point = System.Windows.Point;
- using Rect = System.Windows.Rect;
- namespace HTJ.Models;
- [AppHelper.Service]
- public partial class TestModel : ObservableObject, IUser
- {
- [NotifyPropertyChangedFor(nameof(NameStr))]
- [ObservableProperty] private string _name = "KZ";
- [NotifyPropertyChangedFor(nameof(NameStr))]
- [NotifyPropertyChangedFor(nameof(Geo))]
- [ObservableProperty] private double _value = 10;
-
- [NotifyPropertyChangedFor(nameof(RadiusCent))]
- [ObservableProperty] private double _radius = 10;
-
- [ObservableProperty] private double _thick = 10;
-
- [ObservableProperty] private double _ang = 10;
-
- [ObservableProperty] private double _x = 10;
- [ObservableProperty] private double _y = 10;
-
- public double RadiusCent => -0.5*Radius;
-
- public System.Windows.Media.Color Color { get; set; } = Colors.Blue;
- public string NameStr => Name + Value;
- public GeometryGroup Geo => GetGeo();
-
- private GeometryGroup GetGeo()
- {
- var geos = new GeometryGroup();
-
- // 添加多个几何图形到组中
- geos.Children.Add(new EllipseGeometry(new Point(50, 50), Value, 80));
-
-
- geos.Children.Add(new RectangleGeometry(new Rect(0, 0, 20, 20)));
-
- // 添加一条线
- var line = new LineGeometry(new Point(200, 200), new Point(300, 100));
- geos.Children.Add(line);
-
- // 添加自定义路径
- var customPath = new PathGeometry();
-
- var figure = new PathFigure();
- figure.StartPoint = new Point(150, 150);
- figure.Segments.Add(new LineSegment(new Point(200, 200), true));
- figure.Segments.Add(new LineSegment(new Point(100, 200), true));
- figure.IsClosed = true;
- figure.IsFilled = true;
- customPath.Figures.Add(figure);
-
- geos.Children.Add(customPath);
-
- return geos;
- }
- // 随机线条颜色
-
-
- }

- <Window x:Class="HTJ.Views.TestView"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:vm="clr-namespace:HTJ.ViewModels"
- mc:Ignorable="d"
- Topmost="True"
- Title="TestView" Height="500" Width="400">
- <Window.DataContext>
- <vm:TestViewModel />
- </Window.DataContext>
- <Window.Resources>
- <ResourceDictionary>
- <ResourceDictionary.MergedDictionaries>
- <ResourceDictionary Source="..\styles\styles.xaml" />
- </ResourceDictionary.MergedDictionaries>
- </ResourceDictionary>
- </Window.Resources>
- <StackPanel Margin="10">
- <TextBox Margin="0,2" Text="{Binding VmData.Name,UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource LabelTextBox}" Height="30" Tag="字符串" />
- <TextBox Margin="0,2" Text="{Binding VmData.Value,UpdateSourceTrigger=PropertyChanged}" Height="30" Tag="100" />
- <Separator Height="20" Background="Red" />
- <StackPanel Orientation="Horizontal">
- <TextBlock Text="" FontSize="14" FontWeight="Bold" Margin="0,3" />
- <TextBlock Text="{Binding VmData.NameStr}" FontSize="14" FontWeight="Bold" Margin="0,3" HorizontalAlignment="Right" />
- </StackPanel>
- <Separator Height="20" Background="Red" />
- <StackPanel Orientation="Horizontal">
- <Button Content="拾取" Command="{Binding SelCommand}" />
- <Button Content="添加" Command="{Binding AddCommand}" />
- <Button Content="保存" Command="{Binding SaveCommand}" />
- <Button Content="重载" Command="{Binding ReadCommand}" />
- <Button Content="打开" Command="{Binding OpenCommand}" />
- <Button Content="渲染" Command="{Binding RenderCommand}" />
- <Button Content="停止" Command="{Binding StopCommand}" />
- </StackPanel>
- <Canvas Background="SandyBrown"
- Width="300" Height="300"
- HorizontalAlignment="Left"
- DataContext="{Binding VmData}">
- <TextBlock Text="{Binding NameStr}"
- Canvas.Left="{Binding X}"
- Canvas.Top="{Binding Y}"
- FontSize="15"
- FontStyle="Normal"
- FontWeight="Bold"
- Foreground="{Binding Color}">
- <TextBlock.RenderTransform>
- <RotateTransform Angle="{Binding Ang}"/>
- </TextBlock.RenderTransform>
- </TextBlock>
- <Ellipse Width="250" Height="250"
- Canvas.Left="150" Canvas.Top="150"
- StrokeThickness="1"
- Stroke="White">
- <Ellipse.RenderTransform>
- <TranslateTransform X="-125"
- Y="-125">
- </TranslateTransform>
- </Ellipse.RenderTransform>
- </Ellipse>
- <Ellipse Width="{Binding Radius}"
- Height="{Binding Radius}"
- Canvas.Left="150" Canvas.Top="150"
- StrokeThickness="{Binding Thick}"
- StrokeDashArray="1,4"
- Stroke="Red">
- <Ellipse.RenderTransform>
- <TranslateTransform X="{Binding RadiusCent}"
- Y="{Binding RadiusCent}">
- </TranslateTransform>
- </Ellipse.RenderTransform>
- </Ellipse>
- <Ellipse Width="2" Height="2"
- Canvas.Left="{Binding X}"
- Canvas.Top="{Binding Y}"
- StrokeThickness="5"
- Stroke="GreenYellow">
- <Ellipse.RenderTransform>
- <TranslateTransform X="-1"
- Y="-1">
- </TranslateTransform>
- </Ellipse.RenderTransform>
- </Ellipse>
- <Button Canvas.Right="10" Canvas.Top="10"
- Padding="10,5"
- BorderBrush="Transparent">
- </Button>
- <!-- <Path Data="{Binding Geo}" -->
- <!-- Stroke="Fuchsia" -->
- <!-- StrokeThickness="1" -->
- <!-- Fill="Red"></Path> -->
- </Canvas>
- </StackPanel>
- </Window>

- namespace HTJ.Views;
- [AppHelper.Service]
- public partial class TestView
- {
- public TestView()
- {
- InitializeComponent();
- }
- //窗体关闭事件
- protected override void OnClosing(CancelEventArgs e)
- {
- e.Cancel = true;
- Hide();
- }
- }
网友答: 这是用来做啥的?就动画效果么网友答: 本帖最后由 wang2006zhi 于 2025-10-22 22:55 编辑
910733066 发表于 2025-9-19 21:33
这是用来做啥的?就动画效果么
大部分是做动态数据图的