加载

时间:2016-10-03 16:11:39

标签: windows-runtime uwp winrt-xaml

我在UWP / WinRT中使用过渡来跟随Fast&流畅的UI设计语言。例如,我有StackPanels来定义这样的转换:

    <StackPanel Orientation="Horizontal">
        <StackPanel.ChildrenTransitions>
            <TransitionCollection>
                <EntranceThemeTransition></EntranceThemeTransition>
            </TransitionCollection>
        </StackPanel.ChildrenTransitions>
    </StackPanel>

这很好用,但是当第一次填充StackPanel和添加新元素时,会显示在ChildrenTransition中设置的动画。我只想在添加新元素时显示它。是否有一种简单的方法来禁用动画?我已经考虑过创建一个包含ItemsSource的附加属性并删除ChildrenTransition的内容并在之后重置它,但这看起来并不太优雅。

2 个答案:

答案 0 :(得分:1)

  

是否有一种简单的方法可以禁用动画?

有一种简单的方法可以在后面的代码中执行此操作,只需在此Transition的{​​{1}}事件中添加此Loaded

StackPanel
代码背后的

<StackPanel x:Name="rootStackPanel" Loaded="rootStackPanel_Loaded">
    <!--<StackPanel.ChildrenTransitions>
        <TransitionCollection>
            <EntranceThemeTransition FromVerticalOffset="600" x:Name="enttst">
            </EntranceThemeTransition>
        </TransitionCollection>
    </StackPanel.ChildrenTransitions>-->

    <TextBlock Text="11111" FontSize="30" />

    <TextBlock Text="2222" FontSize="20" Margin="0,5" />

    <Button Content="add new textblock" Click="Button_Click_1" />
</StackPanel>

也许您也认为这种方法不太优雅,但似乎没有方法可以在纯xaml中执行此操作。

答案 1 :(得分:0)

根据Grace Feng的回答,我通过创建附属属性编写了更多XAML风格的解决方案:

public class PanelExtensions : DependencyObject
{
    public static readonly DependencyProperty ChildrenTransitionsAfterLoadProperty =
        DependencyProperty.Register("ChildrenTransitionsAfterLoad", typeof(TransitionCollection),
            typeof(Panel), new PropertyMetadata(new TransitionCollection()));

    public static TransitionCollection GetChildrenTransitionsAfterLoad(DependencyObject d)
    {
        return (TransitionCollection)d.GetValue(ChildrenTransitionsAfterLoadProperty);
    }

    public static void SetChildrenTransitionsAfterLoad(DependencyObject d, TransitionCollection collection)
    {
        d.SetValue(ChildrenTransitionsAfterLoadProperty, collection);
        var panel = d as Panel;
        if (panel == null)
            return;
        panel.Loaded += PanelOnLoaded;
    }

    private static void PanelOnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        (sender as Panel).ChildrenTransitions = GetChildrenTransitionsAfterLoad(sender as DependencyObject);
    }
}

可以像这样使用:

<StackPanel Orientation="Horizontal">
    <yourNamespace:PanelExtensions.ChildrenTransitionsAfterLoad>
        <TransitionCollection>
            <EntranceThemeTransition></EntranceThemeTransition>
        </TransitionCollection>
    </yourNamespace:PanelExtensions.ChildrenTransitionsAfterLoad>
</StackPanel>