简体   繁体   English

页面过渡

[英]Page Transitions

I have a problem with transitions at the moment using http://www.codeproject.com/KB/WPF/WpfPageTransitions.aspx 我目前使用http://www.codeproject.com/KB/WPF/WpfPageTransitions.aspx进行转换时遇到问题

The problem is that if I have a button on UserControl1 and when the button is pressed, it triggers UserControl2 to transition, but when this happens, the background of UserControl2 is visible but the other things like text and buttons are merged together with UserControl1 . 问题是,如果我在UserControl1上有一个按钮,并且按下该按钮时,它将触发UserControl2转换,但是当发生这种情况时, UserControl2的背景是​​可见的,但其他内容(如文本和按钮)与UserControl1合并在一起。

How can I either apply a transition to UserControl1 so that only UserControl2 is shown? 如何将过渡应用于UserControl1以便仅显示UserControl2

Modified code: 修改后的代码:

NewPage.xml NewPage.xml

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

PageTransition.xaml.cs 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;
    }
}

To achieve this do the following: 为此,请执行以下操作:

  1. Keep a Global reference to the Current and Previous UserControls 保持对当前和以前的用户控件的全局引用
  2. Get your transition as normal but ad a completed event to the DoubleAnimation 正常进行过渡,但向DoubleAnimation投放完成事件
  3. Make the Current UserControl Visible and set the ZIndex higher than the Previous UserControl 使当前用户控件可见,并将ZIndex设置为高于上一个用户控件
  4. Make the Previous UserControl Visivle ans set the ZIndex lover than the Current UserControl 使上一个UserControl Visivle并设置ZIndex lover比当前UserControl
  5. Start the animation 开始动画
  6. Set the Visual Brush to the Previous UserControl 将可视笔刷设置为上一个UserControl
  7. Remove the effect from the Current UserControl 从当前用户控件中删除效果
  8. Now when the animations finished 现在,当动画结束时
  9. Set the ZIndex of the Current UserControl to what you set the Previous UserControl 将当前用户控件的ZIndex设置为您设置先前用户控件的ZIndex
  10. Set the Previous UserControl to Visible 将以前的用户控件设置为可见
  11. Set the Previous UserControl to the Current UserControl 将上一个UserControl设置为当前UserControl

This is MY modified version of this transition library so you may have to tweak it to your needs 这是此过渡库的修改后的版本,因此您可能必须根据需要进行调整

Globals 全球

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

Methods 方法

    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;
    }

Hope this helps you out. 希望这可以帮助你。 Let me know if you have any questions. 如果您有任何疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM