无法找到Storyboard的TargetElement

时间:2015-04-20 16:59:59

标签: c# xaml storyboard windows-store-apps

在我的Windows 8.1商店应用程序中,我试图通过为它们定义翻译变换然后为变换设置动画来水平移动2个UI元素。下面是我的场景的简化XAML:

MyUserControl.Xaml

    <UserControl
        x:Class="AnimTest.UserControls.MyUserControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Grid Background="BlueViolet"/>
    </UserControl>

MainPage.xaml中

    <Page
        x:Class="AnimTest.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:userControls="using:AnimTest.UserControls">

        <Grid x:Name="RootGrid">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <Grid.Resources>
                <Storyboard x:Name="MoveContentGrid">
                    <DoubleAnimation Storyboard.TargetName="ContentGridTransform"
                                     Storyboard.TargetProperty="X"
                                     By="250"
                                     Duration="0:0:0.2"
                                     EnableDependentAnimation="True"/>
                </Storyboard>
                <Storyboard x:Name="MoveMyUserControl">
                    <DoubleAnimation Storyboard.TargetName="MyUserControlTransform"
                                     Storyboard.TargetProperty="X"
                                     By="250"
                                     Duration="0:0:0.2"
                                     EnableDependentAnimation="True"/>
                </Storyboard>
            </Grid.Resources>

            <Grid Grid.Row="0" Background="Blue">
                <Grid.RenderTransform>
                    <TranslateTransform x:Name="ContentGridTransform" />
                </Grid.RenderTransform>
            </Grid>

            <userControls:MyUserControl Width="250" HorizontalAlignment="Left" Grid.Row="0">
                <userControls:MyUserControl.RenderTransform>
                    <TranslateTransform x:Name="MyUserControlTransform" X="-250" />
                </userControls:MyUserControl.RenderTransform>
            </userControls:MyUserControl>

            <Button Content="Animate" Grid.Row="1" Click="AnimateButton_OnClick"/>
        </Grid>
    </Page>

MainPage.xaml.cs中

    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;

    namespace AnimTest
    {
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }

            private void AnimateButton_OnClick(object sender, RoutedEventArgs e)
            {
                MoveContentGrid.Begin();
                MoveMyUserControl.Begin();
            }
        }
    }

当我点击Animate按钮时,我得到以下异常:

  

System.Exception:未检测到已安装的组件。

     

无法解析TargetName MyUserControlTransform。在   Windows.UI.Xaml.Media.Animation.Storyboard.Begin()at   AnimTest.MainPage.AnimateButton_OnClick(Object sender,RoutedEventArgs   E)

但是如果我从后面的代码中注释掉 MoveMyUserControl.Begin(); ,那么对 MoveContentGrid.Begin(); 的调用就可以正常运行并且故事板开始了它的动画。

知道为什么第二次调用导致此异常以及如何使其工作?

0 个答案:

没有答案