首页 技术 正文
技术 2022年11月8日
0 收藏 788 点赞 1,308 浏览 5596 个字

最近项目里面有下拉刷新的需求,自己做了一个,效果还不错。

  <Style TargetType="local:PullToRefreshControl">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<Grid>
<StackPanel VerticalAlignment="Center" Orientation="Horizontal" Visibility="{Binding IsReachThreshold,Converter={StaticResource InversedBooleanToVisibilityConverter}}">
<FontIcon FontSize="" FontFamily="Segoe UI Emoji" Glyph="↓" IsHitTestVisible="False" VerticalAlignment="Bottom"/>
<TextBlock Margin="5,0,5,0" Text="下拉刷新" VerticalAlignment="Bottom"/>
</StackPanel>
<StackPanel VerticalAlignment="Center" Orientation="Horizontal" Visibility="{Binding IsReachThreshold, Converter={StaticResource BooleanToVisibilityConverter}}">
<FontIcon FontSize="" FontFamily="Segoe UI Emoji" Glyph="↑" IsHitTestVisible="False" VerticalAlignment="Bottom"/>
<TextBlock Margin="5,0,5,0" Text="释放立即刷新" VerticalAlignment="Bottom"/>
</StackPanel>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:PullToRefreshControl">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Margin="{TemplateBinding Margin}">
<ScrollViewer x:Name="ScrollViewer"
VerticalScrollBarVisibility="Hidden">
<StackPanel>
<ContentControl x:Name="PanelHeader" ContentTemplate="{TemplateBinding HeaderTemplate}" HorizontalContentAlignment="Center" VerticalContentAlignment="Bottom" />
<ContentPresenter x:Name="PanelContent" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</StackPanel>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
 [TemplatePart(Name = PanelHeader, Type = typeof(ContentControl))]
[TemplatePart(Name = PanelContent, Type = typeof(ContentPresenter))]
[TemplatePart(Name = ScrollViewer, Type = typeof(ScrollViewer))]
public class PullToRefreshControl:ContentControl
{
#region Fields
private const string PanelHeader = "PanelHeader";
private const string PanelContent = "PanelContent";
private const string ScrollViewer = "ScrollViewer";
private ContentControl _panelHeader;
private ContentPresenter _panelContent;
private ScrollViewer _scrollViewer;
#endregion #region Property /// <summary>
/// The threshold for release to refresh,defautl value is 2/5 of PullToRefreshPanel's height.
/// </summary>
public double RefreshThreshold
{
get { return (double)GetValue(RefreshThresholdProperty); }
set { SetValue(RefreshThresholdProperty, value); }
} // Using a DependencyProperty as the backing store for RefreshThreshold. This enables animation, styling, binding, etc...
public static readonly DependencyProperty RefreshThresholdProperty =
DependencyProperty.Register("RefreshThreshold", typeof(double), typeof(PullToRefreshControl), new PropertyMetadata(0.0,new PropertyChangedCallback(OnRefreshThresholdChanged))); private static void OnRefreshThresholdChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var pullToRefreshControl = d as PullToRefreshControl;
pullToRefreshControl.UpdateContentGrid();
} /// <summary>
/// occur when reach threshold.
/// </summary>
public event EventHandler PullToRefresh; public DataTemplate HeaderTemplate
{
get { return (DataTemplate)GetValue(HeaderTemplateProperty); }
set { SetValue(HeaderTemplateProperty, value); }
} // Using a DependencyProperty as the backing store for HeaderTemplate. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HeaderTemplateProperty =
DependencyProperty.Register("HeaderTemplate", typeof(DataTemplate), typeof(PullToRefreshControl), new PropertyMetadata(null)); public bool IsReachThreshold
{
get { return (bool)GetValue(IsReachThresholdProperty); }
set { SetValue(IsReachThresholdProperty, value); }
} // Using a DependencyProperty as the backing store for IsReachThreshold. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsReachThresholdProperty =
DependencyProperty.Register("IsReachThreshold", typeof(bool), typeof(PullToRefreshControl), new PropertyMetadata(false)); #endregion protected override void OnApplyTemplate()
{
_panelHeader = GetTemplateChild(PanelHeader) as ContentControl;
_panelHeader.DataContext = this;
_panelContent = GetTemplateChild(PanelContent) as ContentPresenter;
_scrollViewer = GetTemplateChild(ScrollViewer) as ScrollViewer;
_scrollViewer.ViewChanged += _scrollViewer_ViewChanged;
base.OnApplyTemplate();
} private void _scrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
//Sometime we can't make it to 0.0.
IsReachThreshold = _scrollViewer.VerticalOffset <= 5.0;
if (e.IsIntermediate)
{
return;
} if (IsReachThreshold)
{
if (PullToRefresh!=null)
{
PullToRefresh(this, null);
}
}
_panelHeader.Height = RefreshThreshold > _panelHeader.ActualHeight ? RefreshThreshold : _panelHeader.ActualHeight;
this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
_scrollViewer.ChangeView(null, _panelHeader.Height, null);
}); } public PullToRefreshControl()
{
this.DefaultStyleKey = typeof(PullToRefreshControl);
this.Loaded +=(s,e)=>
{
if (RefreshThreshold == 0.0)
{
RefreshThreshold = this.ActualHeight * / 5.0;
}
UpdateContentGrid();
};
this.SizeChanged += (s, e) =>
{
if (RefreshThreshold==0.0)
{
RefreshThreshold = this.ActualHeight * / 5.0;
}
UpdateContentGrid();
};
} #region Method
private void UpdateContentGrid()
{
if (_scrollViewer != null && _panelContent!=null && _panelHeader !=null)
{
_panelHeader.Height = RefreshThreshold > _panelHeader.ActualHeight? RefreshThreshold: _panelHeader.ActualHeight;
this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
_scrollViewer.ChangeView(null, _panelHeader.Height, null, true);
});
_panelContent.Width = this.ActualWidth;
_panelContent.Height = this.ActualHeight;
}
}
#endregion
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,892