2014-06-11 38 views
2

我想使用ObjectAnimationUsingKeyFrames在winrt中爲我的一個控件創建動畫。在一段時間內,控制應該從保證金=「0,0,-500,0」移動到保證金=「0,0,0,0」。我有以下代碼。它幾乎可以工作,但一切都立即發生,沒有看到動畫或平滑過渡。ObjectAnimationUsingKeyFrames - WinRT中位置之間的平滑過渡

<ObjectAnimationUsingKeyFrames Storyboard.TargetName="NameOfControl" 
            Storyboard.TargetProperty="(FrameworkElement.Margin)" 
            Duration="00:00:00.5"> 
     <DiscreteObjectKeyFrame KeyTime="0"> 
      <DiscreteObjectKeyFrame.Value> 
       <Thickness>0,0,-500,0</Thickness> 
      </DiscreteObjectKeyFrame.Value> 
     </DiscreteObjectKeyFrame> 
     <DiscreteObjectKeyFrame KeyTime="00:00:0.5"> 
      <DiscreteObjectKeyFrame.Value> 
       <Thickness>0,0,0,0</Thickness> 
      </DiscreteObjectKeyFrame.Value> 
     </DiscreteObjectKeyFrame> 
    </ObjectAnimationUsingKeyFrames> 
  • 你有任何線索,爲什麼它不工作嗎?
  • 你有另一種方式 如何將控制從某個面板外部移動到其默認的 位置?在我看來,使用保證金是它的一種破解。
+1

我不是一個WinRT的專家,但是這是我想嘗試:不是動畫的'Margin',把你的控制了'Canvas'內使用'DoubleAnimation'動畫'Canvas.Left' 。你可以在Charles Petzold的文章[「Animating Attached Properties」中找到一個例子](http://www.charlespetzold.com/blog/2007/09/080231.html)。所有必需的類似乎在WinRT中都可用。 –

回答

0

我想,我發現它。看這個代碼。 (它的動作控制下跌100點和100點到右)

<DoubleAnimationUsingKeyFrames Storyboard.TargetName="TextBlock1" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)"> 
    <EasingDoubleKeyFrame KeyTime="0" Value="0"/> 
    <EasingDoubleKeyFrame KeyTime="0:0:1" Value="100"/> 
</DoubleAnimationUsingKeyFrames> 

<DoubleAnimationUsingKeyFrames Storyboard.TargetName="TextBlock1" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)"> 
    <EasingDoubleKeyFrame KeyTime="0" Value="0"/> 
    <EasingDoubleKeyFrame KeyTime="0:0:1" Value="100"/> 
</DoubleAnimationUsingKeyFrames> 

但你不能忘記添加該代碼來控制你想移動。

<TextBlock Name="TextBlock1" Text="SomeText"> 
    <TextBlock.RenderTransform> 
     <CompositeTransform/> 
    </TextBlock.RenderTransform> 
</TextBlock> 
0

我懷疑你在代碼中設置的持續時間和keyFrame太少了,這就是爲什麼你沒有感覺到轉換。請增加持續時間和關鍵幀,並讓我知道結果。

+0

謝謝你的回覆。我也嘗試了更長的時間,但它的行爲方式相同。 –

1

問題是與ObjectAnimationUsingKeyFrames & DiscreteObjectKeyFrame u的使用,它不會動畫的值,而是它在所提到的關鍵幀時所設置的值作爲整體,

爲了動畫像保證金,填充等屬性。使用ThicknessAnimationUsingKeyFramesSplineThicknessKeyFrame代替,這將在一個平穩的過渡

樣品動畫值,你

<ThicknessAnimationUsingKeyFrames Storyboard.TargetName="NameOfControl" 
           Storyboard.TargetProperty="(FrameworkElement.Margin)" 
           Duration="00:00:00.5"> 
    <SplineThicknessKeyFrame KeyTime="00:00:00" Value="0,0,-500,0" /> 
    <SplineThicknessKeyFrame KeyTime="00:00:00.5" Value="0,0,0,0" /> 
</ThicknessAnimationUsingKeyFrames> 

WinRT的替代

,因爲你正在使用的有關元素使用右邊距

我試圖創建一個樣本,將採用雙動畫模擬類似的行爲,使其認爲

<Rectangle Fill="Red" 
       Width="10" 
       Height="10" 
       HorizontalAlignment="Center" 
       VerticalAlignment="Center"> 
     <Rectangle.RenderTransform> 
      <TranslateTransform x:Name="translate"/> 
     </Rectangle.RenderTransform> 
     <Rectangle.Triggers> 
      <EventTrigger RoutedEvent="Loaded"> 
       <BeginStoryboard> 
        <Storyboard> 
         <DoubleAnimation Duration="00:00:00.5" 
             Storyboard.TargetProperty="X" 
             Storyboard.TargetName="translate" 
             From="500" 
             To="0"/> 
        </Storyboard> 
       </BeginStoryboard> 
      </EventTrigger> 
     </Rectangle.Triggers> 
    </Rectangle> 

它也不是一個明智的想法動畫保證金屬性,因爲它也強制佈局再次完成

+0

「ThicknessAnimationUsingKeyFrames」不幸在WinRT下不可用。 –

+0

是的,這就是關鍵,我需要它在WinRT –

+0

II作爲WPF寫了答案,因爲從問題的標題或描述中不清楚,直到我在你提到這裏之後看到一個用於winrt的小標籤。對於同樣的道歉。我已經爲WinRT的另一種方法更新了我的答案 – pushpraj