如大佬所言,WinUI 中的 ProgressRing 不能在较老的系统版本中渲染。有其它实现方法,如 WpfUI,但其的关键帧又没调好。
我很早就获得了正确的关键帧,并使用 SVG 实现了此动画。但由于对 xaml 和 C# 均不熟悉,故只能将 WpfUI 的实现扔给 AI 让其修改为正确的关键帧并转为 Powershell 脚本,虽然仍然不能直接用在 UWP 上,但还是希望有所帮助= =
代码:
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName PresentationCore
Add-Type -AssemblyName WindowsBase
$cx = 16.933332
$cy = 16.933332
$radius = 9.4701843
$strokeThickness = 3.0
$bgColor = [System.Windows.Media.Color]::FromArgb(128, 0xC2, 0xC4, 0xCA)
$fgColor = [System.Windows.Media.Color]::FromRgb(0x00, 0x67, 0xC0)
$window = New-Object System.Windows.Window
$window.Width = 220; $window.Height = 220
$window.WindowStartupLocation = 'CenterScreen'
$window.Background = [System.Windows.Media.Brushes]::White
$canvas = New-Object System.Windows.Controls.Canvas
$canvas.Width = 33.866666; $canvas.Height = 33.866668
$viewbox = New-Object System.Windows.Controls.Viewbox
$viewbox.Width = 160; $viewbox.Height = 160
$viewbox.Child = $canvas
$window.Content = $viewbox
function New-ArcPathFigure([double]$startAngleDeg, [double]$endAngleDeg, [double]$r) {
$toPt = {
param($a)
$rad = $a * [Math]::PI / 180.0
New-Object System.Windows.Point(($r + $r * [Math]::Sin($rad)), ($r - $r * [Math]::Cos($rad)))
}
$startPt = & $toPt $startAngleDeg
$endPt = & $toPt $endAngleDeg
$seg = New-Object System.Windows.Media.ArcSegment
$seg.Size = New-Object System.Windows.Size($r, $r)
$seg.IsLargeArc = ([Math]::Abs($endAngleDeg - $startAngleDeg) -gt 180)
$seg.SweepDirection = [System.Windows.Media.SweepDirection]::Clockwise
$seg.Point = $endPt
$fig = New-Object System.Windows.Media.PathFigure
$fig.StartPoint = $startPt
[void]$fig.Segments.Add($seg)
return @{ Figure = $fig; Segment = $seg }
}
# ---- background cover ring (static, EndAngle ~359°) ----
$bgParts = New-ArcPathFigure 0 359 $radius
$bgGeo = New-Object System.Windows.Media.PathGeometry
[void]$bgGeo.Figures.Add($bgParts.Figure)
$bg = New-Object System.Windows.Shapes.Path
$bg.Data = $bgGeo
$bg.Stroke = New-Object System.Windows.Media.SolidColorBrush($bgColor)
$bg.StrokeThickness = $strokeThickness
$bg.StrokeStartLineCap = [System.Windows.Media.PenLineCap]::Round
$bg.StrokeEndLineCap = [System.Windows.Media.PenLineCap]::Round
[System.Windows.Controls.Canvas]::SetLeft($bg, $cx - $radius)
[System.Windows.Controls.Canvas]::SetTop($bg, $cy - $radius)
[void]$canvas.Children.Add($bg)
# ---- rotor container (rotated as a whole, RenderTransformOrigin = center) ----
$rotor = New-Object System.Windows.Controls.Canvas
$rotor.Width = 2 * $radius; $rotor.Height = 2 * $radius
$rotor.RenderTransformOrigin = New-Object System.Windows.Point(0.5, 0.5)
[System.Windows.Controls.Canvas]::SetLeft($rotor, $cx - $radius)
[System.Windows.Controls.Canvas]::SetTop($rotor, $cy - $radius)
[void]$canvas.Children.Add($rotor)
$rotate = New-Object System.Windows.Media.RotateTransform
$rotor.RenderTransform = $rotate
# ---- foreground arc (StartAngle = 0, EndAngle animated 100..320) ----
$fgParts = New-ArcPathFigure 0 100 $radius
$fgGeo = New-Object System.Windows.Media.PathGeometry
[void]$fgGeo.Figures.Add($fgParts.Figure)
$arc = New-Object System.Windows.Shapes.Path
$arc.Data = $fgGeo
$arc.Stroke = New-Object System.Windows.Media.SolidColorBrush($fgColor)
$arc.StrokeThickness = $strokeThickness
$arc.StrokeStartLineCap = [System.Windows.Media.PenLineCap]::Round
$arc.StrokeEndLineCap = [System.Windows.Media.PenLineCap]::Round
[void]$rotor.Children.Add($arc)
# ---- rotation: keyframe animation 0 -> 450 -> 1080 over 2s, forever ----
$rotAnim = New-Object System.Windows.Media.Animation.DoubleAnimationUsingKeyFrames
$rotAnim.Duration = New-Object System.Windows.Duration([TimeSpan]::FromSeconds(2))
$rotAnim.RepeatBehavior = [System.Windows.Media.Animation.RepeatBehavior]::Forever
# 关键帧 1:0 秒,角度 0
$kf1 = New-Object System.Windows.Media.Animation.LinearDoubleKeyFrame
$kf1.KeyTime = [System.Windows.Media.Animation.KeyTime]::FromTimeSpan([TimeSpan]::FromSeconds(0))
$kf1.Value = 0
[void]$rotAnim.KeyFrames.Add($kf1)
# 关键帧 2:1 秒,角度 450
$kf2 = New-Object System.Windows.Media.Animation.LinearDoubleKeyFrame
$kf2.KeyTime = [System.Windows.Media.Animation.KeyTime]::FromTimeSpan([TimeSpan]::FromSeconds(1))
$kf2.Value = 450
[void]$rotAnim.KeyFrames.Add($kf2)
# 关键帧 3:2 秒,角度 1080
$kf3 = New-Object System.Windows.Media.Animation.LinearDoubleKeyFrame
$kf3.KeyTime = [System.Windows.Media.Animation.KeyTime]::FromTimeSpan([TimeSpan]::FromSeconds(2))
$kf3.Value = 1080
[void]$rotAnim.KeyFrames.Add($kf3)
$rotate.BeginAnimation([System.Windows.Media.RotateTransform]::AngleProperty, $rotAnim)
# ---- sweep growth: animate an angle DOUBLE (100 <-> 320), autoreverse, 5s ----
# WPF has no direct "AngleAnimation"; use a DependencyObject property (Opacity is reused
# here purely as an animatable double holder) and recompute the ArcSegment point per frame.
$angleHolder = New-Object System.Windows.Controls.Grid # dummy element to host the animated double
$angleAnim = New-Object System.Windows.Media.Animation.DoubleAnimationUsingKeyFrames
$angleAnim.Duration = New-Object System.Windows.Duration([TimeSpan]::FromSeconds(2))
$angleAnim.RepeatBehavior = [System.Windows.Media.Animation.RepeatBehavior]::Forever
# 关键帧 1:0 秒,角度 0.05
$kf1 = New-Object System.Windows.Media.Animation.LinearDoubleKeyFrame
$kf1.KeyTime = [System.Windows.Media.Animation.KeyTime]::FromTimeSpan([TimeSpan]::FromSeconds(0))
$kf1.Value = 0.05
[void]$angleAnim.KeyFrames.Add($kf1)
# 关键帧 2:1 秒,角度 180
$kf2 = New-Object System.Windows.Media.Animation.LinearDoubleKeyFrame
$kf2.KeyTime = [System.Windows.Media.Animation.KeyTime]::FromTimeSpan([TimeSpan]::FromSeconds(1))
$kf2.Value = 180
[void]$angleAnim.KeyFrames.Add($kf2)
# 关键帧 3:2 秒,角度 0.05
$kf3 = New-Object System.Windows.Media.Animation.LinearDoubleKeyFrame
$kf3.KeyTime = [System.Windows.Media.Animation.KeyTime]::FromTimeSpan([TimeSpan]::FromSeconds(2))
$kf3.Value = 0.05
[void]$angleAnim.KeyFrames.Add($kf3)
$angleHolder.BeginAnimation([System.Windows.Controls.Grid]::OpacityProperty, $angleAnim)
$onRender = {
$endAngleDeg = $angleHolder.Opacity # current animated angle, reused via Opacity (0-320 exceeds 0..1 but WPF allows out-of-range Opacity for animation-only usage; clamp visually is irrelevant since Grid isn't rendered)
$rad = $endAngleDeg * [Math]::PI / 180.0
$fgParts.Segment.Point = New-Object System.Windows.Point(($radius + $radius * [Math]::Sin($rad)), ($radius - $radius * [Math]::Cos($rad)))
# $fgParts.Segment.IsLargeArc = ([Math]::Abs($endAngleDeg) -gt 180)
}
[System.Windows.Media.CompositionTarget]::add_Rendering($onRender)
[void]$window.ShowDialog()
粘贴到 Powershell 中运行即可。
效果: