【发布时间】:2014-03-15 08:35:17
【问题描述】:
我为自己创建了一个用于选取框的小型原型。它将标签从屏幕的右边缘移动到左侧。到了那里,它又从右边开始,所以它看起来是无限的。就像新闻行情一样。
我的问题是,标签的位置移动非常模糊。它只是显得粗鲁和颠簸。我每 33 毫秒将标签向左移动 1.0。
我的渲染器在硬件端(第 2 层)运行。 框架:4.5
XAML:
<Window x:Class="Canny.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="811" Name="main">
<Canvas Name="myCanvas" Background="Black">
<Label Name="lb1" Content="Lauftext+++" Foreground="WhiteSmoke" FontSize="25" Canvas.Left="618" Canvas.Top="10" FontFamily="Courier New" FontWeight="ExtraBold" FontStretch="UltraExpanded"/>
</Canvas>
XAML.CS:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
namespace Canny
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DispatcherTimer timer;
double newPos;
readonly double actualWidthLb;
int renderingTier;
public MainWindow()
{
InitializeComponent();
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(33.0);
timer.Tick += timer_Tick;
newPos = myCanvas.ActualWidth;
actualWidthLb = lb1.ActualWidth;
timer.Start();
renderingTier = (RenderCapability.Tier >> 16);
}
void timer_Tick(object sender, EventArgs e)
{
newPos -= 1.0;
Canvas.SetLeft(lb1,newPos);
if (Canvas.GetLeft(lb1) + actualWidthLb <= 0)
{
Canvas.SetLeft(lb1, myCanvas.ActualWidth);
newPos = myCanvas.ActualWidth;
}
}
}
}
提前感谢您的建议!
【问题讨论】:
-
在 WPF 中你应该使用动画来实现你想要的。
-
很难做到这一点,人眼对这样的运动非常敏感。进化的生存特性,让你远离饥饿的狮子。也让人们发疯,这是一个从不在 UI 中使用它的好理由。 33 毫秒大约是您可以选择的最差值,计时器在 Windows 上以 15.625 毫秒的倍数的间隔滴答作响。好的值是它的倍数,四舍五入。所以 15、31、46。这仍然会导致您偶尔错过一个滴答声,请务必始终使用实际经过的时间来计算增量。
-
嗨,Hans Passant,“确保始终使用实际经过的时间来计算增量”是什么意思。 ?
-
只是不要假设计时器始终按照您要求的速率计时。有时会迟到,这是不可避免的。如果您忽略这一点,那么运动将变得生涩。因此,请使用秒表来了解自上次滴答以来实际 过去了多少时间。乘以所需的速度以获得增量。
-
对不起汉斯帕桑特,但我想我不明白你的意思。我必须实现这样的秒表:pasteall.org,但效果不佳。