2013-11-04 28 views
2

我有一個需求,我需要一個按鈕在幾秒鐘內從屏幕的一端移動到另一端(水平)。我嘗試了這個,並在一定程度上起作用。XAML:從代碼背後設置Storyboard動畫的值

<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="button"> 
      <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0" /> 
      <!--<EasingDoubleKeyFrame KeyTime="0:0:6.9" Value="{Binding MainPage.width}">--> 
       <EasingDoubleKeyFrame KeyTime="0:0:6.9" Value="-1283.725"> 
       <EasingDoubleKeyFrame.EasingFunction> 
        <CircleEase EasingMode="EaseIn"/> 
       </EasingDoubleKeyFrame.EasingFunction> 
      </EasingDoubleKeyFrame> 
     </DoubleAnimationUsingKeyFrames> 
     <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)" Storyboard.TargetName="button"> 
      <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0"/> 
      <!--<EasingDoubleKeyFrame KeyTime="0:0:6.9" Value="{Binding Path=MainPage.height}">--> 
      <EasingDoubleKeyFrame KeyTime="0:0:6.9" Value="-467.732"> 
       <EasingDoubleKeyFrame.EasingFunction> 
        <CircleEase EasingMode="EaseIn"/> 
       </EasingDoubleKeyFrame.EasingFunction> 
      </EasingDoubleKeyFrame> 
     </DoubleAnimationUsingKeyFrames> 

但問題是由於屏幕大小爲&屏幕方向。 在大屏幕上停在中間。我想設置的值 EasingDoubleKeyFrame動態。代碼後面或綁定或任何其他方式。

但我不想在Code Behind中寫出整個故事板。

我可以用

Rect bounds = Window.Current.Bounds; 
    static double height = 0; 
    static double width = 0; 

    public MainPage() 
    { 
     this.InitializeComponent(); 
     this.Loaded += MainPage_Loaded; 
     height = bounds.Height; 
     width = -1 * bounds.Width; 
    } 

讓我知道,如果有一個簡單的方法獲取屏幕寬度&高度。

回答

4

我找不到一種方法來綁定故事板表達式的值。

但是,解決方法是每次手動設置它。我們可以通過給它像一個name屬性做到這一點:

<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform). (CompositeTransform.TranslateX)" Storyboard.TargetName="button"> 
     <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0" /> 
     <EasingDoubleKeyFrame x:Name="buttonTranslateX" KeyTime="0:0:9.9" Value="0" /> 
    </DoubleAnimationUsingKeyFrames> 

而且在頁面初始化設置這樣的價值:

Rect bounds = Window.Current.Bounds; 
static double height = 0; 
static double width = 0; 

public MainPage() 
{ 
    this.InitializeComponent(); 
    this.Loaded += MainPage_Loaded; 
    height = bounds.Height; 
    width = -1 * bounds.Width; 
    buttonTranslateX.Value = width; 
} 

這將創建一個漂亮的小動畫,其中一個對象會向左右方向移動屏幕在9秒左右。

相關問題