views:

332

answers:

1

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:

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.

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); }
  }
}

Here is an example of the animation code :

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();
}

What am I missing?

+2  A: 

Your xaml file needs a reference to your namespace in order to find your AniScrollViewer

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>
Arcturus
Great , it is working now, Thanks