void MainPage_Loaded(object sender, RoutedEventArgs e) { #region 回到顶部按钮 // Visual States are always on the first child of the control template FrameworkElement element = VisualTreeHelper.GetChild(viewer_hot, 0) as FrameworkElement; if (element != null) { VisualStateGroup group = FindVisualState(element, "ScrollStates"); if (group != null) { //group.CurrentStateChanging += (s, args) => pivot.Title = args.NewState.Name; group.CurrentStateChanging += (s, args) => { if (args.NewState.Name == "Scrolling") { imgTopBack.Visibility = System.Windows.Visibility.Collapsed; } else if (viewer_hot.VerticalOffset > 1) { imgTopBack.Visibility = System.Windows.Visibility.Visible; } }; } } #endregion }
VisualStateGroup FindVisualState(FrameworkElement element, string name) { if (element == null) return null; IList groups = VisualStateManager.GetVisualStateGroups(element); foreach (VisualStateGroup group in groups) if (group.Name == name) return group; return null; }
private void imgTopBack_Tap(object sender, System.Windows.Input.GestureEventArgs e) { viewer_hot.ScrollToVerticalOffset(0.0); imgTopBack.Visibility = Visibility.Collapsed; e.Handled = true; }
xaml 中:
<Image Height="56" Width="56" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="12,0,0,80" Name="imgTopBack" Source="/images/topBack.png" Stretch="Fill" Visibility="Collapsed" Tap="imgTopBack_Tap" />