将XAML转换为动态控制代码

时间:2012-01-05 16:20:52

标签: silverlight xaml

设计器创建了一些XAML VisualState代码,在构建为XAML时可以正常工作。现在我的任务是将此XAML转换为Code Behind,这是一个继承自ChildWindow的自定义模板控件,它将构建动画以在各种“窗口”之间进行转换,这些窗口都是子ChildWindow的子UserControl。

我编写了所有代码,但是当我运行任何动画时,我得到一个错误,Silverlight“无法解析targetproperty”(UIElement.Projection)。(PlaneProjection.RotationY)'“

这是原始的XAML

<VisualState x:Name="AtRegistration">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="WindowContentPresenter" Storyboard.TargetProperty="(Content).Children[0].(UIElement.Visibility)">
            <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="Collapsed" />
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="WindowContentPresenter" Storyboard.TargetProperty="(Content).Children[1].(UIElement.Visibility)">
            <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="Visible" />
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>
<VisualState x:Name="AtLogin">
    <Storyboard>
        ... XAML omitted ...
    </Storyboard>
</VisualState>
<VisualStateGroup.Transitions>
    <VisualTransition From="AtRegistration" To="AtLogin">
        <Storyboard>
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ContentRoot" Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)">
                ... XAML OMITTED ...
            </DoubleAnimationUsingKeyFrames>
            ... XAML OMITTED ...
        </Storyboard>
    </VisualTransition>
    <VisualTransition From="AtLogin" To="AtRegistration">
        ... XAML OMITTED ...
    </VisualTransition>
</VisualStateGroup.Transitions>

                                                                                                            ...... XAML OMITTED ......     

上面的XAML工作正常,但是当我编写代码时,却没有。我假设我在代码中做错了,但我找不到它。

public override void OnApplyTemplate()
{
base.OnApplyTemplate();

var content = (ContentPresenter)GetTemplateChild( "WindowContentPresenter" );
m_contentRoot = (Grid)GetTemplateChild( "ContentRoot" );

var grid = (Grid)GetTemplateChild( "Root" );

if( grid != null )
{
    // Create the state group
    var animGroup = new VisualStateGroup();
    animGroup.SetValue( NameProperty, "AnimationStates" );

    var grp = VisualStateManager.GetVisualStateGroups( grid );
    grp.Add( animGroup );

    for( int i = 0; i < MultiControls.Count; i++ )
    {
        var state = new VisualState();
        state.SetValue( NameProperty, GetControlName( i ) );
        animGroup.States.Add( state );

        state.Storyboard = new Storyboard();

        // create an animation for each of the multi controls
        for( int j = 0; j < MultiControls.Count; j ++ )
        {
            // Create the storyboard
            var anim = new ObjectAnimationUsingKeyFrames();
            Storyboard.SetTarget( state.Storyboard, content );
            anim.SetValue( Storyboard.TargetPropertyProperty, new PropertyPath( string.Format( "(Content).Children[{0}].(UIElement.Visibility)", j ) ) );
            anim.KeyFrames.Add( new DiscreteObjectKeyFrame
                                    {
                                        KeyTime = KeyTime.FromTimeSpan( new TimeSpan( 0, 0, 0 ) ),
                                        Value = j == i ? Visibility.Visible : Visibility.Collapsed
                                    } );

            state.Storyboard.Children.Add( anim );

            // Don't create a transition to and from the other states.
            if( i == j )
                continue;

            // Create the Transition
            var trans = new VisualTransition { From = GetControlName( j ), To = GetControlName( i ) };
            animGroup.Transitions.Add( trans );
            trans.Storyboard = new Storyboard();

            var dbl = new DoubleAnimationUsingKeyFrames { BeginTime = TimeSpan.FromSeconds( 0 ) };
            Storyboard.SetTarget( trans.Storyboard, m_contentRoot );
            dbl.SetValue( Storyboard.TargetPropertyProperty, new PropertyPath( "(UIElement.Projection).(PlaneProjection.RotationY)" ) );
            trans.Storyboard.Children.Add( dbl );

            ... CODE OMITTED ...
        }
    }
}
}

在我的模板XAML中,我有这个:

<Grid x:Name="Overlay" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="0" Background="{TemplateBinding OverlayBrush}" Opacity="{TemplateBinding OverlayOpacity}"/>
<Grid x:Name="ContentRoot" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}" RenderTransformOrigin="0.5,0.5" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}">
<Grid.Projection>
    <PlaneProjection/>
</Grid.Projection>

你们/女士们有什么想法吗?

感谢。

1 个答案:

答案 0 :(得分:0)

Storyboard.SetTarget的第二个参数应该是投影,而不是m_contentroot。然后,您需要在路径中指定的是“RotationY”。