如何将扩展类 ScrollViewer 添加到 XAML 文件? [英] How do I add an extended class ScrollViewer to the XAML file?
问题描述
我一直在寻找一种方法来为 ScrollViewer 的滚动设置动画,我找到了一个示例,但是当我尝试将类添加到 XAML 文件时,出现错误:
I was looking for a way to animate the scrolling of a ScrollViewer and I found a sample, but when I try to add the class to the XAML file I get an error:
错误2
'AniScrollViewer' 类型没有找到.确认您不是缺少程序集引用,并且所有引用的程序集都已已建成.
Error 2
The type 'AniScrollViewer' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
这是我在论坛中找到的代码,并将该类添加到我的 cs 文件中:
this is the code I found in a forum and I added the class to my cs file:
public class AniScrollViewer:ScrollViewer
{
public static DependencyProperty CurrentVerticalOffsetProperty = DependencyProperty.Register("CurrentVerticalOffset", typeof(double), typeof(AniScrollViewer), new PropertyMetadata(new PropertyChangedCallback(OnVerticalChanged)));
public static DependencyProperty CurrentHorizontalOffsetProperty = DependencyProperty.Register("CurrentHorizontalOffsetOffset", typeof(double), typeof(AniScrollViewer), new PropertyMetadata(new PropertyChangedCallback(OnHorizontalChanged)));
private static void OnVerticalChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
AniScrollViewer viewer = d as AniScrollViewer;
viewer.ScrollToVerticalOffset((double)e.NewValue);
}
private static void OnHorizontalChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
AniScrollViewer viewer = d as AniScrollViewer;
viewer.ScrollToHorizontalOffset((double)e.NewValue);
}
public double CurrentHorizontalOffset
{
get { return (double)this.GetValue(CurrentHorizontalOffsetProperty); }
set { this.SetValue(CurrentHorizontalOffsetProperty, value); }
}
public double CurrentVerticalOffset
{
get { return (double)this.GetValue(CurrentVerticalOffsetProperty); }
set { this.SetValue(CurrentVerticalOffsetProperty, value); }
}
}
这是动画代码的示例:
private void ScrollToPosition(double x, double y)
{
DoubleAnimation vertAnim = new DoubleAnimation();
vertAnim.From = MainScrollViewer.VerticalOffset;
vertAnim.To = y;
vertAnim.DecelerationRatio = .2;
vertAnim.Duration = new Duration(TimeSpan.FromMilliseconds(250));
DoubleAnimation horzAnim = new DoubleAnimation();
horzAnim.From = MainScrollViewer.HorizontalOffset;
horzAnim.To = x;
horzAnim.DecelerationRatio = .2;
horzAnim.Duration = new Duration(TimeSpan.FromMilliseconds(300));
Storyboard sb = new Storyboard();
sb.Children.Add(vertAnim);
sb.Children.Add(horzAnim);
Storyboard.SetTarget(vertAnim, MainScrollViewer);
Storyboard.SetTargetProperty(vertAnim, new PropertyPath(AniScrollViewer.CurrentVerticalOffsetProperty));
Storyboard.SetTarget(horzAnim, MainScrollViewer);
Storyboard.SetTargetProperty(horzAnim, new PropertyPath(AniScrollViewer.CurrentHorizontalOffsetProperty));
sb.Begin();
}
我错过了什么?
推荐答案
你的 xaml 文件需要对命名空间的引用才能找到你的 AniScrollViewer
Your xaml file needs a reference to your namespace in order to find your AniScrollViewer
假设您的 AniScrollViewer 位于命名空间 Test 中,您可以像这样在 xaml 中使用它:
Lets say, your AniScrollViewer is located in namespace Test, you can use it in your xaml like so:
<Window x:Class="something"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Test="clr-namespace:Test;assembly=">
<Test:AniScrollViewer />
</Window>
这篇关于如何将扩展类 ScrollViewer 添加到 XAML 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!