页面过渡

时间:2012-01-15 14:48:21

标签: c# wpf

我使用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 个答案:

答案 0 :(得分:1)

要实现此目的,请执行以下操作:

  1. 保持对当前和以前用户控件的全局引用
  2. 正常进行转换,但将完成的事件添加到DoubleAnimation
  3. 使当前UserControl可见,并将ZIndex设置为高于Previous UserControl
  4. 让以前的UserControl Visivle设置ZIndex爱好者而不是当前用户控件
  5. 启动动画
  6. 将Visual Brush设置为Previous UserControl
  7. 从当前用户控件中删除效果
  8. 现在动画完成时
  9. 将当前UserControl的ZIndex设置为您设置Previous UserControl的
  10. 将Previous UserControl设置为Visible
  11. 将Previous UserControl设置为Current UserControl
  12. 这是此过渡库的 MY 修改版本,因此您可能需要根据需要进行调整

    全局

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

    希望这会帮助你。如果您有任何问题,请告诉我。