2015-06-17 145 views
0

我正在製作側欄風格的應用程序,位於屏幕右側的左側。 我試圖讓它看起來像滑入和滑出屏幕,但是當它在右側時,它仍然從左側滑入。動畫窗口寬度從右到左

我找不到任何關於窗口寬度的權利,所以我希望在這裏找到答案。

這是我目前的工作動畫代碼爲從左到右:

public static void AnimateSize(this Window target, double newWidth, EventHandler completed) 
    { 
     var length = new TimeSpan(0, 0, 0, 0, Settings.Default.AnimationTime); 
     var sb = new Storyboard {Duration = new Duration(length)}; 

     var aniWidth = new DoubleAnimationUsingKeyFrames(); 

     aniWidth.Duration = new Duration(length); 

     aniWidth.KeyFrames.Add(new EasingDoubleKeyFrame(target.ActualWidth, 
      KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, 00)))); 
     aniWidth.KeyFrames.Add(new EasingDoubleKeyFrame(newWidth, KeyTime.FromTimeSpan(length))); 

     Storyboard.SetTarget(aniWidth, target); 
     Storyboard.SetTargetProperty(aniWidth, new PropertyPath(FrameworkElement.WidthProperty)); 

     sb.Children.Add(aniWidth); 

     sb.Completed += completed; 
     sb.Begin(); 
    } 

這樣調用:

_window.AnimateSize(0, delegate { _window.Hide(); }); 

這:

_window.Width = 0; 
_window.Show(); 
_window.AnimateSize(Settings.Default.Width, delegate { }); 

感謝。

回答

2
 private void Animate(double beginfrom, double to, DependencyProperty dp) 
     { 
      var da = new DoubleAnimation { 
       From = beginfrom, 
       To = to, 
       FillBehavior = FillBehavior.Stop, 
       Duration = new Duration(TimeSpan.FromSeconds(0.3)), 
       AccelerationRatio = 0.1 
      }; 
      var storyBoard = new Storyboard(); 
      storyBoard.Children.Add(da); 
      Storyboard.SetTarget(da, this); //this = your control object 
      Storyboard.SetTargetProperty(da, new PropertyPath(dp)); 
      storyBoard.Begin(); 
     } 

簡單的用法:Animate(0, 250, WidthProperty);

+0

我不得不調整它,否則它會顯示給有多個顯示器的用戶。 – user3476998

+0

你只需要改變依賴屬性。 – 0x3h

1

我有類似的任務來完成。我用我的動畫窗口的左側財產,使它看起來好像它從外面滑動並以同樣的方式消失。但是當一個使用兩個屏幕的同事使用它時,窗口會滑動到另一個屏幕上。

周圍使用的工作是同時動畫的寬度和left屬性。這給窗口出現在期望的位置和向左揚起的效果。

DoubleAnimation daLeftAppear = new DoubleAnimation(popupWidthDetails.workAreaWidth, (<workAreaWidth> - <windowWidth> - 5), new Duration(TimeSpan.FromSeconds(1))); 
     daLeftAppear.EasingFunction = new QuarticEase(); 
     Storyboard.SetTargetName(daLeftAppear, this.Name); 
     Storyboard.SetTargetProperty(daLeftAppear, new PropertyPath(LeftProperty)); 

     DoubleAnimation daWidthAppear = new DoubleAnimation(0, 100, new Duration(TimeSpan.FromSeconds(1))); 
     daWidthAppear.EasingFunction = new QuarticEase(); 
     Storyboard.SetTargetName(daWidthAppear, this.Name); 
     Storyboard.SetTargetProperty(daWidthAppear, new PropertyPath(WidthProperty)); 

     sbAppear = new Storyboard(); 
     sbAppear.Children.Add(daLeftAppear); 
     sbAppear.Children.Add(daWidthAppear);