0

在过去的几天里,我一直在努力尝试为拿着物品的情况下获得漂亮的前景动画效果。

项目模板如下所示:

<DataTemplate>
    <StackPanel toolkit:TiltEffect.IsTiltEnabled="True" Hold="OnLongListSelectorItemHold">
        <toolkit:ContextMenuService.ContextMenu>
            <toolkit:ContextMenu>
                <toolkit:MenuItem Header="edit" />
                <toolkit:MenuItem Header="delete" />
            </toolkit:ContextMenu>
        </toolkit:ContextMenuService.ContextMenu>
        <TextBlock x:Name="SubjectTextBlock" Text="{Binding Subject}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Last modified :" Margin="15, 0, 5, 0" Foreground="LightGray" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock Text="{Binding LastModified}" Foreground="#989696" Style="{StaticResource PhoneTextNormalStyle}"/>
        </StackPanel>
    </StackPanel>
</DataTemplate>

我尝试了多种方法,但我没有设法从其中任何一种方法中得到结果。

我遇到了这个很好的 MSDN 帖子,其中显示了多个示例,但没有一个与我的情况真正匹配,因为TextBlock我想要动画的前景TextBlock,在 a 中引用 s,DataTemplate所以我在访问模板中的特定控件时遇到问题。

例如,我尝试了这种方法:

<phone:PhoneApplicationPage.Resources>
    <Storyboard x:Name="ItemHoldAnimation">
        <ColorAnimation Storyboard.TargetName="SubjectTextBlock" 
                        Storyboard.TargetProperty="Foreground"
                        From="White" To="{StaticResource PhoneAccentColor}" Duration="0:00:04"/>
    </Storyboard>
</phone:PhoneApplicationPage.Resources>

然后从Hold事件处理程序中触发它:

var storyboard = Resources["ItemHoldAnimation"] as Storyboard;
storyboard.Begin();

但是它失败了,TargetName="SubjectTextBlock"因为它在DataTemplate...内部,因此无法访问

我还尝试了一种我为 WPF 找到的方法EventTriggers,如下所示:

<StackPanel toolkit:TiltEffect.IsTiltEnabled="True" Hold="OnLongListSelectorItemHold">
    <StackPanel.Triggers>
        <EventTrigger RoutedEvent="StackPanel.Hold">
            <BeginStoryboard Storyboard="{StaticResource ItemHoldAnimation}"/>
        </EventTrigger>
    </StackPanel.Triggers>
    ...
</StackPanel>

但它给了COM异常......

MS.Internal.WrappedException: Error HRESULT E_FAIL has been returned from a call to a COM component. ---> System.Exception: Error HRESULT E_FAIL has been returned from a call to a COM component.

很多只是为了在LongListSelector项目被持有时为字体设置动画......

解决这个问题的方法是什么?

4

1 回答 1

3

您应该在 DataTemplate 中定义情节提要,还需要修改目标属性,因为 ColorAnimation 在 Color 属性上工作而不是画笔。最后IsZoomEnabled="False"也需要设置,否则 ContextMenu 实现会在上下文菜单打开时对元素进行快照并显示此静态图像,因此动画将不可见(替代方法是修改 ContextMenu 的源代码以延迟打开动画完成后的上下文菜单)这样的东西应该可以工作:

<DataTemplate x:Key="dataTemplate">
        <StackPanel toolkit:TiltEffect.IsTiltEnabled="True" Hold="OnLongListSelectorItemHold">
            <StackPanel.Resources>
                <Storyboard x:Name="ItemHoldAnimation">
                    <ColorAnimation Storyboard.TargetName="SubjectTextBlock" 
                    Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                    From="White" To="{StaticResource PhoneAccentColor}" Duration="0:00:04"/>
                </Storyboard>
                <Storyboard x:Name="MenuClosedAnimation">
                    <ColorAnimation Storyboard.TargetName="SubjectTextBlock" 
                Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                From="{StaticResource PhoneAccentColor}" To="White" Duration="0:00:04"/>
                </Storyboard>
            </StackPanel.Resources>
            <toolkit:ContextMenuService.ContextMenu>
                <toolkit:ContextMenu IsZoomEnabled="False" Closed="ContextMenu_OnClosed">
                    <toolkit:MenuItem Header="edit" />
                    <toolkit:MenuItem Header="delete" />
                </toolkit:ContextMenu>
            </toolkit:ContextMenuService.ContextMenu>
            <TextBlock x:Name="SubjectTextBlock" Text="Test" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Last modified :" Margin="15, 0, 5, 0" Foreground="LightGray" Style="{StaticResource PhoneTextNormalStyle}"/>
                <TextBlock Text="{Binding LastModified}" Foreground="#989696" Style="{StaticResource PhoneTextNormalStyle}"/>
            </StackPanel>
        </StackPanel>
    </DataTemplate>

这是保持方法:

private void OnLongListSelectorItemHold(object sender, GestureEventArgs e)
    {
        FrameworkElement fe = sender as FrameworkElement;
        var storyboard = fe.Resources["ItemHoldAnimation"] as Storyboard;
        storyboard.Begin();
    }

private void ContextMenu_OnClosed(object sender, RoutedEventArgs e)
    {
        ContextMenu eleme=sender as ContextMenu;
        FrameworkElement fe = eleme.Owner as FrameworkElement;
        var storyboard = fe.Resources["MenuClosedAnimation"] as Storyboard;
        storyboard.Begin();
    }
于 2013-09-29T11:28:50.977 回答