How do I add an extended class ScrollViewer to the XAML file?

你。 提交于 2019-12-11 09:09:01

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/672465/how-do-i-add-an-extended-class-scrollviewer-to-the-xaml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!