我有一个 Pivot 控件,它具有三个 PivotItems,它们都是 LongListSelectors,现在我需要在首先加载的任何枢轴项目上显示特定类型的动画,即在启动或导航时向用户显示的项目到页面。为此,我已将加载的事件附加到枢轴控件,该事件在加载控件时为我提供显示的 PivotItem,并基于此我将 DataTemplates 提供给枢轴项内的 LongListSelecor
private async void MainPivot_Loaded(object sender, RoutedEventArgs e)
{
var sen = (Pivot)sender;
for (int k = 0; k < sen.Items.Count; k++)
{
if (k == sen.SelectedIndex)
{
var navigatingTo = (LongListSelector)((PivotItem)((Pivot)sender).SelectedItem).Content;
navigatingTo.ItemTemplate = Resources["AnimationDataTemplate"] as DataTemplate;
}
else
((LongListSelector)((PivotItem)sen.Items[k]).Content).ItemTemplate = Resources["DefaultDataTemplate"] as DataTemplate;
}
}
我有两个 DataTemplates 一个没有动画
<DataTemplate x:Key="DefaultDataTemplate">
<StackPanel x:Name="Panel" Background="#f5f5f5" Width="430" Margin="2,0,0,10">
<StackPanel Orientation="Horizontal">
<Rectangle Fill="{Binding Condition, Converter={StaticResource conditionToColor}}" Height="Auto" Width="5"/>
<StackPanel Margin="10,10,0,10" Height="{Binding Converter={StaticResource conditionToStackPanelHeight}}" Width="330">
<TextBlock x:Name="Text1" Text="{Binding LineOne}" Foreground="#5ac8d5" FontFamily="Lato" FontWeight="SemiBold" FontSize="{Binding Converter={StaticResource conditionToMainTextFontSize}}" Width="330" TextAlignment="Left" TextWrapping="Wrap" Margin="{Binding Converter={StaticResource conditionToMainTextMargin}}" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock x:Name="Text2" Text="Hospital" Foreground="#8fa7b3" FontSize="{Binding Converter={StaticResource conditionToSubTextFontSize}}" Width="330" TextAlignment="Left" TextWrapping="Wrap" Margin="{Binding Converter={StaticResource conditionToSubTextMargin}}" Style="{StaticResource PhoneTextSubtleStyle}"/>
<TextBlock x:Name="Text3" Text="Visited on" Foreground="#8fa7b3" FontSize="{Binding Converter={StaticResource conditionToSubTextFontSize}}" Width="330" TextAlignment="Left" TextWrapping="Wrap" Margin="{Binding Converter={StaticResource conditionToSubTextMargin}}" Style="{StaticResource PhoneTextSubtleStyle}"/>
<TextBlock x:Name="Text4" Text="By" Foreground="#8fa7b3" FontSize="{Binding Converter={StaticResource conditionToSubTextFontSize}}" Width="330" TextAlignment="Left" TextWrapping="Wrap" Margin="{Binding Converter={StaticResource conditionToSubTextMargin}}" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
<Image Source="Assets/Images/SmallMale.png" Height="{Binding Converter={StaticResource conditionToImageHeight}}" Margin="{Binding Converter={StaticResource conditionToImageMargin}}" Opacity="0.25" VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
</StackPanel>
</StackPanel>
</DataTemplate>
另一个有动画
<DataTemplate x:Key="AnimationDataTemplate">
<StackPanel x:Name="PanelToAnimate" Background="#f5f5f5" Width="430" Margin="622,0,-622,10" Loaded="PanelToAnimate_Loaded" Unloaded="PanelToAnimate_Unloaded">
<StackPanel.Resources>
<Storyboard x:Name="myStackPanelboard">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.GlobalOffsetX)" Storyboard.TargetName="PanelToAnimate">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="-620"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Name="EndStoryBoard">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.GlobalOffsetX)" Storyboard.TargetName="PanelToAnimate">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="620"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</StackPanel.Resources>
<StackPanel.Projection>
<PlaneProjection/>
</StackPanel.Projection>
<StackPanel Orientation="Horizontal">
<Rectangle Fill="{Binding Condition, Converter={StaticResource conditionToColor}}" Height="Auto" Width="5"/>
<StackPanel Margin="10,10,0,10" Height="{Binding Converter={StaticResource conditionToStackPanelHeight}}" Width="330">
<TextBlock x:Name="Text1" Text="{Binding LineOne}" Foreground="#5ac8d5" FontFamily="Lato" FontWeight="SemiBold" FontSize="{Binding Converter={StaticResource conditionToMainTextFontSize}}" Width="330" TextAlignment="Left" TextWrapping="Wrap" Margin="{Binding Converter={StaticResource conditionToMainTextMargin}}" Style="{StaticResource PhoneTextExtraLargeStyle}">
<TextBlock.Projection>
<PlaneProjection/>
</TextBlock.Projection>
</TextBlock>
<TextBlock x:Name="Text2" Text="Hospital" Foreground="#8fa7b3" FontSize="{Binding Converter={StaticResource conditionToSubTextFontSize}}" Width="330" TextAlignment="Left" TextWrapping="Wrap" Margin="{Binding Converter={StaticResource conditionToSubTextMargin}}" Style="{StaticResource PhoneTextSubtleStyle}">
<TextBlock.Projection>
<PlaneProjection/>
</TextBlock.Projection>
</TextBlock>
<TextBlock x:Name="Text3" Text="Visited on" Foreground="#8fa7b3" FontSize="{Binding Converter={StaticResource conditionToSubTextFontSize}}" Width="330" TextAlignment="Left" TextWrapping="Wrap" Margin="{Binding Converter={StaticResource conditionToSubTextMargin}}" Style="{StaticResource PhoneTextSubtleStyle}">
<TextBlock.Projection>
<PlaneProjection/>
</TextBlock.Projection>
</TextBlock>
<TextBlock x:Name="Text4" Text="By" Foreground="#8fa7b3" FontSize="{Binding Converter={StaticResource conditionToSubTextFontSize}}" Width="330" TextAlignment="Left" TextWrapping="Wrap" Margin="{Binding Converter={StaticResource conditionToSubTextMargin}}" Style="{StaticResource PhoneTextSubtleStyle}">
<TextBlock.Projection>
<PlaneProjection/>
</TextBlock.Projection>
</TextBlock>
</StackPanel>
<Image Source="Assets/Images/SmallMale.png" Height="{Binding Converter={StaticResource conditionToImageHeight}}" Margin="{Binding Converter={StaticResource conditionToImageMargin}}" Opacity="0.25" VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
</StackPanel>
</StackPanel>
</DataTemplate>
“AnimationDataTemplate”内的 StackPanel 有一个加载的事件,在该事件中我执行开始故事板的逻辑,当应用程序启动时,我已经收到 PanelToAnimate_Loaded 事件 7 次,这是预期的如果所有 longlistselectors 中有 7 个项目,现在如果我导航到(比如说我在第一个 PivotItem LongListSelector 中,我从那里转到第二个 PivotItem LongListSelector 并从那里导航)并从下一页返回,我会触发 PanelToAnimate_Loaded 事件 14 次,但 longlistselector 中的项目是 7。
有人可以帮我弄这个吗 ?