gpt4 book ai didi

c# - 滚动查看器文本 block 在 WP7 中滚动

转载 作者:行者123 更新时间:2023-11-30 15:05:57 24 4
gpt4 key购买 nike

我有一个绑定(bind)到滚动查看器的文本 block 来滚动文本。当某些事件(如按钮单击)被触发时,我希望文本在没有任何用户输入的情况下自动滚动。我尝试使用 ScrollToVerticalOffset 但过渡并不顺利。无论如何我可以使文本平滑地向上滚动吗?

最佳答案

这是一个示例,其中我创建了一个名为 AnimatableScrollViewer 的包装器控件,它包含一个常用的 ScrollViewer 和一个 TextBlock。

<UserControl>
<Grid x:Name="LayoutRoot" Background="Transparent">
<ScrollViewer x:Name="scrollViewer" Width="{Binding ActualWidth, ElementName=userControl, Mode=OneWay}" Height="{Binding ActualHeight, ElementName=userControl, Mode=OneWay}">
<TextBlock TextWrapping="Wrap" Text="Add some pretty long text here..."/>
</ScrollViewer>
</Grid>
</UserControl>

在代码隐藏中,我添加了一个 DependencyProperty(我们可以从外部对其进行动画处理),它会在每次更改时调用 ScrollViewer 的 ScrollToVerticalOffset() 方法。

public partial class AnimatableScrollViewer : UserControl
{
public static readonly DependencyProperty AnimatablOffsetProperty = DependencyProperty.Register("AnimatableOffset",
typeof(double), typeof(AnimatableScrollViewer), new PropertyMetadata(AnimatableOffsetPropertyChanged));

public double AnimatableOffset
{
get { return (double)this.GetValue(AnimatablOffsetProperty); }
set { this.SetValue(AnimatablOffsetProperty, value); }
}

public AnimatableScrollViewer()
{
InitializeComponent();
AnimatableOffset = scrollViewer.VerticalOffset;
}

private static void AnimatableOffsetPropertyChanged(object sender, DependencyPropertyChangedEventArgs args)
{
AnimatableScrollViewer cThis = sender as AnimatableScrollViewer;
cThis.scrollViewer.ScrollToVerticalOffset((double)args.NewValue);
}
}

现在您可以将 AnimatableScrollViewer 添加到 PhonePage 并为其设置动画。例如在像这样的按钮事件处理程序中:

private void cmdScroll_Click(object sender, RoutedEventArgs e)
{
// Calculate target offset
double targetOffset = 1000;

// Create animation and storyboard
DoubleAnimation animation = new DoubleAnimation();
animation.EasingFunction = new CircleEase();
animation.Duration = new Duration(new TimeSpan(0, 0, 2));
animation.From = animatableScrollViewer.AnimatableOffset;
animation.To = targetOffset;

Storyboard.SetTarget(animation, animatableScrollViewer);
Storyboard.SetTargetProperty(animation, new PropertyPath("(AnimatableScrollViewer.AnimatableOffset)"));
Storyboard storyboard = new Storyboard();
storyboard.Children.Add(animation);

storyboard.Begin();
}

当然,您也可以在 xaml 代码中创建动画,这会使它看起来更简洁。现在,ScrollViewer 的内容是固定的...您可以通过向包装类添加更多依赖属性来使其可变。

我不知道这是否是最好的解决方案,事实上它对我来说看起来很丑陋,但它应该让您了解如何完成。

关于c# - 滚动查看器文本 block 在 WP7 中滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8474236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com