【问题标题】:Animating UserControl XAML动画 UserControl XAML
【发布时间】:2011-11-29 10:38:37
【问题描述】:

我一直在尝试为 UserControl 的打开设置动画,但没有成功,想知道是否有人可以提供帮助?

我有一个 UserControl,它只保存有关记录的信息。它在现有页面上作为一个框打开,但是我希望该框以一个简单的动画打开。我试图让盒子打开一个展开的动画,而不是刚刚出现的盒子。

下面是我一直在处理的代码。

<UserControl Name="RecordViewerUserControl" 
            xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
            x:Class="VisionWare.MasterDataServices.Silverlight.UI.Common.RecordViewer"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
            xmlns:conv="clr-namespace:VisionWare.MasterDataServices.Silverlight.UI.Converters"
            xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
            mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"

            Height="490"
            Width="600"
            Margin="0,0,0,0">





<UserControl.Resources>
    <conv:DateConverter x:Key="dateConverter" />
    <conv:BoolToVisibilityConverter x:Key="visibilityConverter" />
    <conv:EntityIdToUrlConverter x:Key="urlConverter"/>
    <conv:FileConverter x:Key="fileConverter"/>
    <conv:AlertImageURLConverter x:Key="alertConverter"/>

    <Style TargetType="UserControl" x:Key="CustomUserControlStyle">
        <Setter Property="Effect">
            <Setter.Value>
    <BeginStoryboard>
        <Storyboard>
            <DoubleAnimationUsingKeyFrames BeginTime="0" Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="(RenderTransform).(Children)[0].ScaleX">
                <SplineDoubleKeyFrame KeyTime="00:00:00.2" Value="1" />
                <SplineDoubleKeyFrame KeyTime="00:00:00.25" Value="1.05" />
                <SplineDoubleKeyFrame KeyTime="00:00:00.45" Value="0" />
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames BeginTime="0" Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="(RenderTransform).(Children)[0].ScaleY">
                <SplineDoubleKeyFrame KeyTime="00:00:00.2" Value="1" />
                <SplineDoubleKeyFrame KeyTime="00:00:00.25" Value="1.05" />
                <SplineDoubleKeyFrame KeyTime="00:00:00.45" Value="0" />
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </BeginStoryboard>
            </Setter.Value>
        </Setter>
    </Style>

</UserControl.Resources>

我已根据@jrb 的建议更改了我的代码...]

<UserControl.Triggers>
    <EventTrigger RoutedEvent="UserControl.Loaded">
        <EventTrigger.Actions>
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="RecordViewerUserControl" Storyboard.TargetProperty="Width" To="600"/>
                    <DoubleAnimation Storyboard.TargetName="RecordViewerUserControl" Storyboard.TargetProperty="Height" To="490"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger.Actions>
    </EventTrigger>
</UserControl.Triggers>

我在最初的 UserControl 开始标记之后插入了它。 运行应用程序时不再抱怨,但似乎没有效果。

有什么想法吗?我在后面的代码中遗漏了什么吗?

【问题讨论】:

  • 为什么不直接使用ChildWindow?你如何像弹出框一样打开这个UserControl
  • 您好,感谢您的回复。我没有使用 childWindow,因为我需要将焦点保持在弹出的 userControl 后面的主窗口上(您仍然可以在主 UserControl 上选择记录,基本上是一个弹出窗口。
  • 打开这个用户控件的代码是什么?

标签: c# .net xaml silverlight


【解决方案1】:

我认为你的动画值是错误的,最后你的值应该是 1(100% 大小)而不是 0。

为什么要在 LayoutRoot 的第一个孩子上制作动画?您应该将 RecordViewerUserControl 作为动画目标。

【讨论】:

  • 您好,非常感谢您的回复!抱歉,我是 XAML 的初学者。我放了 LayoutRoot,因为我试图定位 UserControl 的全部内容。当我在 RecordViewerUserControl 上制作动画时,它似乎根本没有动画?
  • 把样式放到一个全局的地方进行测试。还可以尝试将帧的 KeyTime 设置为更高的值,以便查看会发生什么。
【解决方案2】:

我建议您测试将用户控件上的大小设置为 1,然后使用加载的事件触发器(我认为)和一个简单的双动画来增加控件的大小。

<EventTrigger RoutedEvent="Loaded">

这是一个工作示例,您可以在 kaxaml 中对其进行测试。

<Page
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     Width="1"
     Height="1"
     Background="Black">
     <Page.Triggers>
          <EventTrigger RoutedEvent="Loaded">
               <EventTrigger.Actions>
                    <BeginStoryboard>
                         <Storyboard>
                              <DoubleAnimation
                                   Storyboard.TargetProperty="Width"
                                   To="200"/>
                              <DoubleAnimation
                                   Storyboard.TargetProperty="Height"
                                   To="200"/>
                         </Storyboard>
                    </BeginStoryboard>
               </EventTrigger.Actions>
          </EventTrigger>
     </Page.Triggers>
     <Grid>
     </Grid>
</Page>

【讨论】:

  • 感谢您的回复!它似乎不喜欢加载的 routedEvent,但谢谢。 :)
  • 您好,再次感谢您。你能指出我把这段代码放在哪里的正确方向吗?我似乎无法让它在 标记中工作。我还尝试在打开窗口时从按钮事件触发器本身调用此代码。
  • 更改用户控件的页面。您还可以将 routedevent 更改为 mouse.click 等。
  • 好的,谢谢。我想我现在可能会有所进展,感谢您的帮助!虽然,您知道我对 RoutedEvent 还有哪些其他选择吗?我似乎找不到很多这样的例子。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-28
  • 2016-08-15
  • 2013-05-12
  • 2012-11-01
相关资源
最近更新 更多