我目前正在使用 Windows Phone 8 应用程序并遇到长列表选择器(LLS)的一些问题。列表框通过使用 TranslateY 值来做到这一点:
UIElement scrollContent = (UIElement)this.targetScrollViewer.Content;
CompositeTransform ct = scrollContent.RenderTransform as CompositeTransform;
//ct.TranslateY: I need this value in Viewport's LLS to detect exactly the distance moving from the TOP
我正在尝试使用 LLS 检测拉动刷新,但它有一些缺陷(使用鼠标输入、移动和离开):
double manipulationStart = 0;
double manipulationEnd = 0;
void targetLLS_MouseEnter(object sender, MouseEventArgs e)
{
if (!this.IsRefreshing)
{
var pos = e.GetPosition(null);
manipulationStart = pos.Y;
IsMoving = false;
}
}
private void targetLLS_MouseMove(object sender, MouseEventArgs e)
{
if (!this.IsRefreshing)
{
var pos = e.GetPosition(null);
manipulationEnd = pos.Y;
IsMoving = true;
double TranslateY = manipulationEnd - manipulationStart;
if (TranslateY > this.PullThreshold)
{
this.PullDistance = 100;
this.PullFraction = 1.0;
activityState = PullDownToRefreshPanel.ReadyToReleaseVisualState;
}
else if (TranslateY > 0)
{
this.PullDistance = 100;
double threshold = this.PullThreshold;
this.PullFraction = 1;// threshold == 0.0 ? 1.0 : Math.Min(1.0, TranslateY / threshold);
activityState = PullDownToRefreshPanel.PullingDownVisualState;
}
else
{
this.PullDistance = 0;
this.PullFraction = 0;
activityState = PullDownToRefreshPanel.InactiveVisualState;
}
VisualStateManager.GoToState(this, activityState, false);
}
}
bool IsMoving = false;
void targetLLS_MouseLeave(object sender, MouseEventArgs e)
{
if (!this.IsRefreshing && IsMoving)
{
double TranslateY = manipulationEnd - manipulationStart;
EventHandler handler = this.RefreshRequested;
if (this.targetLLS.IsAtTop()
&& (activityState == PullDownToRefreshPanel.ReadyToReleaseVisualState))// TranslateY >= this.PullThreshold
{
if (handler != null)
{
IsRefreshing = true;
handler(this, EventArgs.Empty);
}
}
IsMoving = false;
}
PullDistance = 0;
PullFraction = 0;
manipulationStart = 0;
manipulationEnd = 0;
activityState = PullDownToRefreshPanel.InactiveVisualState;
//VisualStateManager.GoToState(this, activityState, false);
}
我该如何使用以下模板:
<Style x:Key="ViewportControlStyle" TargetType="ViewportControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ViewportControl">
<ContentPresenter x:Name="ContentElement" Cursor="{TemplateBinding Cursor}" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="LongListSelectorNormalStyle" TargetType="phone:LongListSelector">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="phone:LongListSelector">
<Grid Background="{TemplateBinding Background}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ScrollStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="00:00:00.5"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Scrolling">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="VerticalScrollBar"/>
</Storyboard>
</VisualState>
<VisualState x:Name="NotScrolling"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid Margin="{TemplateBinding Padding}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ViewportControl x:Name="ViewportControl" HorizontalContentAlignment="Stretch" VerticalAlignment="Top" Style="{StaticResource ViewportControlStyle}"/>
<ScrollBar x:Name="VerticalScrollBar" Grid.Column="0" Margin="4,0,4,0" Opacity="0" Orientation="Vertical" HorizontalAlignment="Right"/>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我的项目需要实现拉功能,所以请帮助我。谢谢阅读!