12

コントロールの状態を変更してから、何らかのアクションを実行する必要があります。具体的には、コントロールが非表示になる前にアニメーションを実行したいです。私はそのようなことをしたいと思います:

VisualStateManager.GoToState(control, "Hidden", true); // wait until the transition animation is finished
ParentControl.Children.Remove(control);

問題は、遷移アニメーションが非同期で実行されるため、アニメーションの開始直後にコントロールがビジュアル ツリーから削除されることです。

では、アニメーションが終了するのをどのように待ちますか?

4

3 に答える 3

14

Storyboard.Completed イベント ハンドラーを Storyboard にアタッチするか、VisualStateGroup.CurrentStateChanged イベント ハンドラーを VisualStateGroup にアタッチできます。

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
x:Class="SilverlightApplication7.MainPage"
Width="640" Height="480">

<Grid x:Name="LayoutRoot" Background="White">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="VisualStateGroup" >
            <VisualState x:Name="Hidden">
                <Storyboard Completed="OnHidden">
                    <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="rectangle" d:IsOptimized="True"/>
                </Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <Rectangle x:Name="rectangle" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="136" Margin="48,72,0,0" Opacity="0" Stroke="Black" VerticalAlignment="Top" Width="208"/>
</Grid>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication7
{
public partial class MainPage : UserControl
{
    public MainPage()
    {
        // Required to initialize variables
        InitializeComponent();

        this.Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        VisualStateManager.GoToState(this, "Hidden", true);
    }

    private void OnHidden(object storyboard, EventArgs args)
    {

    }
}

}

于 2010-09-06T18:39:04.377 に答える
3

この問題を処理する正しい方法は、VisualStateGroup で CurrentStateChanged イベントをリッスンすることですが、私の経験からすると、信頼性が高くなく、最悪の場合壊れています。

2 番目のオプションは、ストーリーボードに Completed イベントをフックすることですが、このオプションには独自の落とし穴があります。場合によっては、Visual State Manager が内部でアニメーションを生成するため、設定した Completed イベントが呼び出されません。

于 2010-09-06T20:35:56.720 に答える
3

実際、Completed ハンドラーをコードでアタッチすることは可能です。

Collection<VisualStateGroup> grps = (Collection<VisualStateGroup>)VisualStateManager.GetVisualStateGroups(this.LayoutRoot);
foreach (VisualStateGroup grp in grps) {
    Collection<VisualState> states = (Collection<VisualState>)grp.States;
    foreach (VisualState state in states) {
        switch (state.Name) {
            case "Intro":
            state.Storyboard.Completed += new EventHandler(Intro_Completed);break;
        }
    }
}

このスレッドの例: http://forums.silverlight.net/forums/p/38027/276746.aspx

添付されたビヘイビアーも使用して、ライブプロジェクトで私のために働いています! 実際の VisualStateGroup コレクションを取得するには、ルート UserControl (VisualStateManager.GoToState で使用するため) と LayoutRoot に個別の依存関係プロパティを使用する必要がありましたが、少し面倒でした。

于 2011-03-17T12:55:56.787 に答える