本帖最后由 wang2006zhi 于 2025-10-23 00:04 编辑
  1. <Window x:Class="HTJ.Views.ProgressView"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6.         xmlns:vm="clr-namespace:HTJ.ViewModels"
  7.         mc:Ignorable="d"
  8.         Height="200"
  9.         Width="500"
  10.         WindowStyle="None"
  11.         AllowsTransparency="True"
  12.         Background="Transparent"
  13.         WindowStartupLocation="CenterScreen"
  14.         Topmost="True" >
  15.     <Window.Resources>
  16.         <!-- 渐变画笔 -->
  17.         <LinearGradientBrush x:Key="PrimaryGradient" StartPoint="0,0" EndPoint="1,1">
  18.             <GradientStop Color="#667eea" Offset="0"/>
  19.             <GradientStop Color="#764ba2" Offset="1"/>
  20.         </LinearGradientBrush>
  21.         <LinearGradientBrush x:Key="ProgressGradient" StartPoint="0,0" EndPoint="1,0">
  22.             <GradientStop Color="#f093fb" Offset="0"/>
  23.             <GradientStop Color="#f5576c" Offset="1"/>
  24.         </LinearGradientBrush>

  25.         <!-- 阴影效果 -->
  26.         <DropShadowEffect x:Key="ShadowEffect" ShadowDepth="0" BlurRadius="20" Color="#40000000" Opacity="0.6"/>
  27.         
  28.         <!-- 进度条光泽效果 -->
  29.         <LinearGradientBrush x:Key="ProgressGlowBrush" StartPoint="0,0" EndPoint="0,1">
  30.             <GradientStop Color="#80FFFFFF" Offset="0.0"/>
  31.             <GradientStop Color="#00FFFFFF" Offset="0.3"/>
  32.             <GradientStop Color="#00FFFFFF" Offset="0.7"/>
  33.             <GradientStop Color="#40FFFFFF" Offset="1.0"/>
  34.         </LinearGradientBrush>
  35.     </Window.Resources>
  36.     <Window.DataContext>
  37.         <vm:ProgressViewModel />
  38.     </Window.DataContext>
  39.     <Border  Margin="20" Background="{StaticResource PrimaryGradient}"
  40.              CornerRadius="15"
  41.              Effect="{StaticResource ShadowEffect}">
  42.         <Grid Margin="20">
  43.             <Grid.RowDefinitions>
  44.                 <RowDefinition Height="Auto"/>
  45.                 <RowDefinition Height="*"/>
  46.                 <RowDefinition Height="Auto"/>
  47.                 <RowDefinition Height="Auto"/>
  48.             </Grid.RowDefinitions>
  49.             
  50.             <!-- 标题栏 -->
  51.             <Grid Grid.Row="0">
  52.                 <Grid.ColumnDefinitions>
  53.                     <ColumnDefinition Width="*"/>
  54.                     <ColumnDefinition Width="Auto"/>
  55.                 </Grid.ColumnDefinitions>
  56.                
  57.                 <!-- 标题-->
  58.                 <TextBlock Text="{Binding VmData.Title}"
  59.                          FontSize="16"
  60.                          FontWeight="Bold"
  61.                          Foreground="White"
  62.                          VerticalAlignment="Center"/>
  63.                
  64.                 <!-- 按钮-->
  65.                 <StackPanel Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Center" >
  66.                     <Button Content="开始" Command="{Binding StartCommand}"  />
  67.                     <Button Content="取消" Command="{Binding CancelCommand}" />
  68.                     <Button Content="重置" Command="{Binding ResetCommand}" />
  69.                 </StackPanel>
  70.             </Grid>
  71.             
  72.             <!-- 当前任务 -->
  73.             <TextBlock Grid.Row="1"
  74.                      Text="{Binding VmData.TxtCurrentTask}"
  75.                      FontSize="12"
  76.                      Foreground="#E0FFFFFF"
  77.                      Margin="0,10,0,5"
  78.                      TextWrapping="Wrap"/>
  79.             
  80.             <!-- 进度条区域 -->
  81.             <ProgressBar Grid.Row="2"
  82.                          Foreground="{StaticResource ProgressGradient}"
  83.                          Background="{StaticResource ProgressGlowBrush}"
  84.                          Value="{Binding VmData.ProgressValue}"
  85.                          Minimum="{Binding VmData.Minimum}"
  86.                          Maximum="{Binding VmData.Maximum}"
  87.                          Height="20" Margin="0,0,0,10" />
  88.             
  89.             <!-- 进度文本和时间 -->
  90.             <Grid Grid.Row="3" Margin="0,10,0,0">
  91.                 <Grid.ColumnDefinitions>
  92.                     <ColumnDefinition Width="*"/>
  93.                     <ColumnDefinition Width="Auto"/>
  94.                 </Grid.ColumnDefinitions>
  95.                 <TextBlock Grid.Column="0"
  96.                            Text="{Binding VmData.TxtProgress}"
  97.                            FontSize="11"
  98.                            Foreground="White"
  99.                            FontWeight="SemiBold"/>
  100.                 <TextBlock Grid.Column="1"
  101.                            Text="{Binding VmData.TxtTimeRemaining}"
  102.                            FontSize="11"
  103.                            Foreground="#C0FFFFFF"/>
  104.             </Grid>
  105.         </Grid>
  106.     </Border>
  107. </Window>
前端
  1. public partial class ProgressView
  2. {
  3.     public ProgressView()
  4.     {
  5.         InitializeComponent();
  6.     }

  7.     //窗体关闭事件
  8.     protected override void OnClosing(CancelEventArgs e)
  9.     {
  10.         e.Cancel = true;
  11.         Hide();
  12.     }
  13. }
复制代码
前端关联
  1. using HTJ.Views;

  2. namespace HTJ.Models;

  3. [AppHelper.Service]
  4. public partial class ProgressModel: ObservableObject
  5. {
  6.     [ObservableProperty] private string _title = "任务处理进度";
  7.    
  8.     [ObservableProperty] private string _txtCurrentTask = "正在初始化..." ;
  9.    
  10.     [ObservableProperty] private string _txtTimeRemaining = "预计剩余: --";
  11.    
  12.     [ObservableProperty]  [NotifyPropertyChangedFor(nameof(TxtProgress))]
  13.    
  14.     private double _progressValue;
  15.    
  16.     [ObservableProperty] private int _closeTime = 3000;
  17.    
  18.     [ObservableProperty] private int _minimum;
  19.    
  20.     [ObservableProperty] private int _maximum = 100;
  21.     public string TxtProgress => $"{ProgressValue*100/Maximum:F0}%";
  22.    
  23.     /// <summary>
  24.     /// 启动自动关闭倒计时
  25.     /// </summary>
  26.     public async Task AutoClose()
  27.     {
  28.         var tokenSource = new CancellationTokenSource();
  29.         
  30.         try
  31.         {
  32.             // 更新剩余时间显示为关闭倒计时
  33.             int seconds = CloseTime / 1000;
  34.             for (int i = seconds; i > 0; i--)
  35.             {
  36.                 if (tokenSource.Token.IsCancellationRequested)
  37.                     break;
  38.                 TxtTimeRemaining = $"窗口将在 {i} 秒后自动关闭...";
  39.                 await Task.Delay(1000, tokenSource.Token);
  40.             }
  41.             if (!tokenSource.Token.IsCancellationRequested)
  42.             {
  43.                 // 关闭窗口
  44.                 var view = AppHelper.GetService<ProgressView>();
  45.                 view.Hide();
  46.             }
  47.         }
  48.         catch (TaskCanceledException)
  49.         {
  50.             // 自动关闭被取消
  51.             TxtTimeRemaining = "自动关闭已取消";
  52.         }
  53.     }
  54. }
数据
  1. using HTJ.Arx.Service.Interface;
  2. using HTJ.Models;
  3. using HTJ.Views;

  4. namespace HTJ.ViewModels;

  5. public partial class ProgressViewModel: ObservableObject
  6. {
  7.     private static readonly IUserSetting IuserSetting = AppX.IuserSetting;

  8.     [ObservableProperty] private ProgressModel _vmData = IuserSetting.GetService<ProgressModel>();
  9.    
  10.     private CancellationTokenSource? _cancellationTokenSource;
  11.    
  12.     [RelayCommand]
  13.     private async Task Start()
  14.     {
  15.         _cancellationTokenSource = new CancellationTokenSource();
  16.         
  17.         try
  18.         {
  19.             VmData.TxtCurrentTask = "开始处理...";
  20.             VmData.ProgressValue = 0;

  21.             // 模拟进度更新
  22.             for (int i = 0; i <= VmData.Maximum; i++)
  23.             {
  24.                 if (_cancellationTokenSource.Token.IsCancellationRequested)
  25.                     break;
  26.                 // 添加小的延迟,让UI有机会更新
  27.                 await Task.Delay(10, _cancellationTokenSource.Token);

  28.                 VmData.ProgressValue = i;
  29.                 VmData.TxtCurrentTask = GetTaskDescription(i);
  30.                 VmData.TxtTimeRemaining = GetRemainingTime(i);
  31.             }

  32.             if (!_cancellationTokenSource.Token.IsCancellationRequested)
  33.             {
  34.                 VmData.TxtCurrentTask = "处理完成!";
  35.                 VmData.TxtTimeRemaining = "剩余时间: 0秒";
  36.                 await AutoCloseAfterDelay(VmData.CloseTime);
  37.             }
  38.         }
  39.         catch (TaskCanceledException)
  40.         {
  41.             VmData.TxtCurrentTask = "操作已取消";
  42.             VmData.TxtTimeRemaining = "已中断";
  43.         }
  44.     }
  45.    
  46.     [RelayCommand]
  47.     private void Cancel()
  48.     {
  49.         _cancellationTokenSource?.Cancel();
  50.     }

  51.     [RelayCommand]
  52.     private void Reset()
  53.     {
  54.         VmData.ProgressValue = 0;
  55.         VmData.TxtCurrentTask = "正在初始化...";
  56.         VmData.TxtTimeRemaining = "预计剩余: --";
  57.         VmData.ProgressValue = 0;
  58.     }
  59.    
  60.     /// <summary>
  61.     /// 计算当前任务状态
  62.     /// </summary>
  63.     /// <param name="progress"></param>
  64.     /// <returns></returns>
  65.     private string GetTaskDescription(int progress)
  66.     {
  67.         return progress switch
  68.         {
  69.             < 20 => "正在加载图层数据...",
  70.             < 40 => "正在分析图层结构...",
  71.             < 60 => "正在处理图层样式...",
  72.             < 80 => "正在优化图层性能...",
  73.             < 100 => "正在保存处理结果...",
  74.             _ => "处理完成"
  75.         };
  76.     }
  77.    
  78.     /// <summary>
  79.     /// 计算剩余时间
  80.     /// </summary>
  81.     /// <param name="progress"></param>
  82.     /// <returns></returns>
  83.     private string GetRemainingTime(int progress)
  84.     {
  85.         if (progress == 0) return "预计剩余: --";
  86.         var remaining = (VmData.Maximum - progress) * 0.1; // 基于模拟延迟计算
  87.         return $"预计剩余: {remaining:F1}秒";
  88.     }
  89.    
  90.     /// <summary>
  91.     /// 启动自动关闭倒计时
  92.     /// </summary>
  93.     /// <param name="delayMilliseconds">延迟毫秒数</param>
  94.     private async Task AutoCloseAfterDelay(int delayMilliseconds)
  95.     {
  96.         var tokenSource = new CancellationTokenSource();
  97.         
  98.         try
  99.         {
  100.             // 更新剩余时间显示为关闭倒计时
  101.             int seconds = delayMilliseconds / 1000;
  102.             for (int i = seconds; i > 0; i--)
  103.             {
  104.                 if (tokenSource.Token.IsCancellationRequested)
  105.                     break;
  106.                 VmData.TxtTimeRemaining = $"窗口将在 {i} 秒后自动关闭...";
  107.                 await Task.Delay(1000, tokenSource.Token);
  108.             }
  109.             if (!tokenSource.Token.IsCancellationRequested)
  110.             {
  111.                 // 触发关闭窗口事件
  112.                 var view = AppHelper.GetService<ProgressView>();
  113.                 view.Hide();
  114.             }
  115.         }
  116.         catch (TaskCanceledException)
  117.         {
  118.             // 自动关闭被取消
  119.             VmData.TxtTimeRemaining = "自动关闭已取消";
  120.         }
  121.     }
  122.    
  123. }
业务逻辑
  1.     [CommandMethod("tt8")]
  2.     public void Tt8()
  3.     {
  4.         var progressModel = AppHelper.GetService<ProgressModel>();
  5.         // 设置进度参数
  6.         progressModel.Title = "图层重命名进度";
  7.         progressModel.CloseTime = 2000;
  8.         progressModel.Maximum = 400;
  9.         progressModel.ProgressValue = 0;
  10.         progressModel.TxtCurrentTask = "开始处理图层重命名...";
  11.         progressModel.TxtTimeRemaining = $"总共 {progressModel.Maximum} 个图层需要处理";
  12.         var view = AppHelper.GetService<ProgressView>();
  13.         view.Show();

  14.     }
复制代码
调用

网友答: 可以。很漂亮!!!

网友答: 相当可以,wpf刚开始学,做出来的空间都很丑

网友答: 哇塞 很炫酷

网友答: 相 当 哇 塞

网友答: 本帖最后由 yanshengjiang 于 2025-10-17 18:53 编辑

漂亮,可以被lsp调用吗

网友答: 驱动文件也发一下,就可以愉快的玩耍了。

网友答: 一看就是热爱生活的

网友答: 干看见很哇塞,不会用
  • 上一篇:Canvas,二开动画
  • 下一篇:没有了