如何使用 MVVM 为社区工具包 RadialGauge 制作动画?
How to animate Community Toolkits RadialGauge with MVVM?
我想为 Windows 社区工具包的 radialgauge 制作动画,但不幸的是 UI 在 RaisePropertyChanged 后不会自行刷新。
这是我的代码:
查看
<TextBlock Grid.Row="0" Text="{Binding RadialGaugeValue}" />
<controls:RadialGauge
x:Name="MyRadialGauge"
Grid.Row="01"
Value="{Binding RadialGaugeValue}"
Minimum="0"
Maximum="100"
StepSize="1"
IsInteractive="False"
TickSpacing="10"
TickWidth="0"
ScaleWidth="4"
ScalePadding="29"
ScaleTickWidth="1"
MinAngle="245"
MaxAngle="115"
Unit="Sparquote"
NeedleWidth="2"
NeedleLength="100"
MaxHeight="250"
Margin="0,20,0,0"
NeedleBrush="{ThemeResource SystemColorHighlightBrush}"
FocusVisualPrimaryBrush="{x:Null}"
FocusVisualSecondaryBrush="Green"
TrailBrush="{ThemeResource SystemColorHotlightBrush}"
ValueStringFormat="."%"">
ViewModel
private async Task DoSavingRateAnimation(double myValue)
{
var progress = new Progress<int>(value =>
{
RadialGaugeValue = value;
RaisePropertyChanged("RadialGaugeValue");
});
await SavingRateAnimation(progress, myValue);
}
private Task SavingRateAnimation(IProgress<int> progress, double value)
{
return Task.Run(() =>
{
int i = 0;
while (RadialGaugeValue < value)
{
Task.Delay(100).Wait();
i += 1;
progress.Report(i);
}
});
}
如您所见,radialGauge 不会自行更新。
你知道我如何为控件设置动画吗?
请设置RadialGauge
值与双向模型的绑定,如下所示。
Value="{Binding RadialGaugeValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
我想为 Windows 社区工具包的 radialgauge 制作动画,但不幸的是 UI 在 RaisePropertyChanged 后不会自行刷新。
这是我的代码:
查看
<TextBlock Grid.Row="0" Text="{Binding RadialGaugeValue}" />
<controls:RadialGauge
x:Name="MyRadialGauge"
Grid.Row="01"
Value="{Binding RadialGaugeValue}"
Minimum="0"
Maximum="100"
StepSize="1"
IsInteractive="False"
TickSpacing="10"
TickWidth="0"
ScaleWidth="4"
ScalePadding="29"
ScaleTickWidth="1"
MinAngle="245"
MaxAngle="115"
Unit="Sparquote"
NeedleWidth="2"
NeedleLength="100"
MaxHeight="250"
Margin="0,20,0,0"
NeedleBrush="{ThemeResource SystemColorHighlightBrush}"
FocusVisualPrimaryBrush="{x:Null}"
FocusVisualSecondaryBrush="Green"
TrailBrush="{ThemeResource SystemColorHotlightBrush}"
ValueStringFormat="."%"">
ViewModel
private async Task DoSavingRateAnimation(double myValue)
{
var progress = new Progress<int>(value =>
{
RadialGaugeValue = value;
RaisePropertyChanged("RadialGaugeValue");
});
await SavingRateAnimation(progress, myValue);
}
private Task SavingRateAnimation(IProgress<int> progress, double value)
{
return Task.Run(() =>
{
int i = 0;
while (RadialGaugeValue < value)
{
Task.Delay(100).Wait();
i += 1;
progress.Report(i);
}
});
}
如您所见,radialGauge 不会自行更新。
你知道我如何为控件设置动画吗?
请设置RadialGauge
值与双向模型的绑定,如下所示。
Value="{Binding RadialGaugeValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"