簡體   English   中英

頁面過渡

[英]Page Transitions

我目前使用http://www.codeproject.com/KB/WPF/WpfPageTransitions.aspx進行轉換時遇到問題

問題是,如果我在UserControl1上有一個按鈕,並且按下該按鈕時,它將觸發UserControl2轉換,但是當發生這種情況時, UserControl2的背景是​​可見的,但其他內容(如文本和按鈕)與UserControl1合並在一起。

如何將過渡應用於UserControl1以便僅顯示UserControl2

修改后的代碼:

NewPage.xml

private void button1_Click(object sender, RoutedEventArgs e)
{
    Test test = new Test();
    pageTransitionControl.SetPreviousUserControl(newPage);
    pageTransitionControl.ShowPage(test);
}

PageTransition.xaml.cs

public partial class PageTransition : UserControl
{
    private UserControl currentUserControl;
    private UserControl previousUserControl;

    public static readonly DependencyProperty TransitionTypeProperty = DependencyProperty.Register("TransitionType",
        typeof(PageTransitionType),
        typeof(PageTransition), new PropertyMetadata(PageTransitionType.SlideAndFade));

    public PageTransitionType TransitionType
    {
        get
        {
            return (PageTransitionType)GetValue(TransitionTypeProperty);
        }
        set
        {
            SetValue(TransitionTypeProperty, value);
        }
    }

    public PageTransition()
    {
        InitializeComponent();
    }

    public void ShowPage(UserControl newPage)
    {
        currentUserControl = newPage;

        if (contentPresenter.Content != null)
        {
            UserControl oldPage = contentPresenter.Content as UserControl;
            oldPage.Loaded -= newPage_Loaded;
            UnloadPage(oldPage);
        }
        else
        {
            ShowNextPage();
        }
    }

    void ShowNextPage()
    {            
        currentUserControl.Loaded += newPage_Loaded;

        contentPresenter.Content = currentUserControl;

        if (currentUserControl != null)
        {
            currentUserControl.Visibility = Visibility.Visible;
            Panel.SetZIndex(currentUserControl, 100);
        }

        if (previousUserControl != null)
        {
            previousUserControl.Visibility = Visibility.Visible;
            Panel.SetZIndex(previousUserControl, 0);      
        }
    }

    void UnloadPage(UserControl page)
    {
        Storyboard hidePage = (Resources[string.Format("{0}Out", TransitionType.ToString())] as Storyboard).Clone();

        hidePage.Completed += hidePage_Completed;

        hidePage.Begin(contentPresenter);
    }

    void newPage_Loaded(object sender, RoutedEventArgs e)
    {
        Storyboard showNewPage = Resources[string.Format("{0}In", TransitionType.ToString())] as Storyboard;

        showNewPage.Begin(contentPresenter);

        currentUserControl = sender as UserControl;
    }

    void hidePage_Completed(object sender, EventArgs e)
    {
        contentPresenter.Content = null;

        ShowNextPage();
    }

    public void SetPreviousUserControl(UserControl userControl)
    {
        previousUserControl = userControl;
    }
}

為此,請執行以下操作:

  1. 保持對當前和以前的用戶控件的全局引用
  2. 正常進行過渡,但向DoubleAnimation投放完成事件
  3. 使當前用戶控件可見,並將ZIndex設置為高於上一個用戶控件
  4. 使上一個UserControl Visivle並設置ZIndex lover比當前UserControl
  5. 開始動畫
  6. 將可視筆刷設置為上一個UserControl
  7. 從當前用戶控件中刪除效果
  8. 現在,當動畫結束時
  9. 將當前用戶控件的ZIndex設置為您設置先前用戶控件的ZIndex
  10. 將以前的用戶控件設置為可見
  11. 將上一個UserControl設置為當前UserControl

這是此過渡庫的修改后的版本,因此您可能必須根據需要進行調整

全球

    private UserControl CurrentUserControl;
    private UserControl PreviousUserControl;
    private Random Random;

方法

    private void TransitionEffectStarting(UserControl userControl)
    {
        CurrentUserControl = userControl;

        TransitionEffect[] effectGroup = Global.TransitionEffects[Random.Next(Global.TransitionEffects.Length)];
        TransitionEffect effect = effectGroup[Random.Next(effectGroup.Length)];

        RandomizedTransitionEffect randomEffect = effect as RandomizedTransitionEffect;
        if (randomEffect != null)
            randomEffect.RandomSeed = Random.NextDouble();

        DoubleAnimation animation = new DoubleAnimation(0.0, 1.0, new Duration(TimeSpan.FromSeconds(1.0)), FillBehavior.HoldEnd);
        animation.AccelerationRatio = 0.5;
        animation.DecelerationRatio = 0.5;
        animation.Completed += TransitionEffectCompleted;

        if (CurrentUserControl != null)
        {
            CurrentUserControl.Visibility = Visibility.Visible;
            Panel.SetZIndex(CurrentUserControl, 1);
        }

        if (PreviousUserControl != null)
        {
            PreviousUserControl.Visibility = Visibility.Visible;
            Panel.SetZIndex(PreviousUserControl, 0);
        }

        else
            Visibility = Visibility.Visible;

        effect.BeginAnimation(TransitionEffect.ProgressProperty, animation);

        if (PreviousUserControl != null)
        {
            VisualBrush visualBrush = new VisualBrush(PreviousUserControl);
            visualBrush.Viewbox = new Rect(0, 0, PreviousUserControl.ActualWidth, PreviousUserControl.ActualHeight);
            visualBrush.ViewboxUnits = BrushMappingMode.Absolute;
            effect.OldImage = visualBrush;
        }

        if (CurrentUserControl != null)
            CurrentUserControl.Effect = effect;
    }

    private void TransitionEffectCompleted(object sender, EventArgs e)
    {
        if (CurrentUserControl != null)
        {
            Panel.SetZIndex(CurrentUserControl, 0);
            CurrentUserControl.Effect = null;

            if (PreviousUserControl != null)
                PreviousUserControl.Visibility = Visibility.Hidden;
        }
        PreviousUserControl = CurrentUserControl;
    }

希望這可以幫助你。 如果您有任何疑問,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM