Je cherchais un moyen d'animer le défilement d'un ScrollViewer et j'ai trouvé un échantillon, mais lorsque je tente d'ajouter la classe au fichier XAML je reçois une erreur:Comment ajouter une classe étendue ScrollViewer au fichier XAML?
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.
c'est le code I found in a forum et J'ai ajouté la classe à mon dossier cs:
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); }
}
}
Voici un exemple du code d'animation:
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();
}
Qu'est-ce que je manque ?
Super, ça marche maintenant, merci –