我想使用十张图片制作自定义动画进度条。现在我使用Timer的Tick来每5毫秒更改一次资源图像:
XAML:
<Grid>
<Image HorizontalAlignment="Stretch"
Margin="0"
VerticalAlignment="Top"
Stretch="Fill"
x:Name="imageProgressBar"/>
</Grid>
C#
private void StartAnimation()
{
if (IsAnimating) return;
_timer = new DispatcherTimer();
_timer.Interval = FrameDuration;
_timer.Tick += TimerTick;
_timer.Start();
}
private void StopAnimation()
{
if (!IsAnimating) return;
_timer.Stop();
_timer = null;
}
private void TimerTick(object sender, object e)
{
if (FramesCount == 0 || _frames == null) return;
_currentFrame++;
_currentFrame = _currentFrame % FramesCount;
if (_currentFrame < _frames.Count)
imageProgressBar.Source = _frames[_currentFrame];
}
这种变体有效,但它不够好,因为它使用UI(我猜)线程进行处理,这就是为什么它有时会变慢。 有没有办法以不同的方式更改图像来源?
答案 0 :(得分:1)
即使我使用我的项目背后的代码创建类似的动画。问题出现在第一个周期,当图像需要被缓冲和加载时需要花费时间,因此动画从第二个周期开始正常工作。
我最终使用的解决方法是在XAML中定义故事板(因为它更容易)并在更新其可见性时在XAML中加载图像
XAML代码:
$('#map').locationpicker({
location: {latitude: position.coords.latitude, longitude: position.coords.longitude},
});