Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Ink;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- using Microsoft.Phone.Controls;
- namespace JeffWilcox.Controls
- {
- // Based upon Headered Content Control as it often comes up in these templates.
- public class WindowBase : HeaderedContentControl
- {
- private PhoneApplicationPage _page;
- private bool _transitionedIn;
- private Panel _templateRoot;
- public event EventHandler PressedBack;
- private Grid _overlay;
- public override void OnApplyTemplate()
- {
- base.OnApplyTemplate();
- _templateRoot = MoreVisualTreeExtensions.FindFirstChildOfType<Panel>(this);
- if (_templateRoot == null)
- {
- throw new InvalidOperationException("Must include a Panel in the root of the template.");
- }
- if (!_transitionedIn)
- {
- var @in = GetTransitionIn();
- if (@in != null)
- {
- var transition = @in.GetTransition(_templateRoot);
- transition.Completed += (x, xe) => transition.Stop();
- transition.Begin();
- }
- _transitionedIn = true;
- }
- }
- private void BuildOverlay()
- {
- var bg = (Color)Resources["PhoneBackgroundColor"];
- _overlay = new Grid();
- _overlay.IsHitTestVisible = true;
- _overlay.Background = new SolidColorBrush(Color.FromArgb(0xa0, bg.R, bg.G, bg.B));
- }
- protected void InsertIntoVisualTree(Panel parentPanel)
- {
- BuildOverlay();
- parentPanel.Children.Add(_overlay);
- parentPanel.Children.Add(this);
- // WARNING: This version will not attach to page back button
- // key presses and could fail ingestion.
- }
- protected void InsertIntoFrame()
- {
- IExposeRootFrame ie = Application.Current as IExposeRootFrame;
- if (ie != null)
- {
- WilcoxTransitionFrame wtf = ie.RootFrame;
- InsertIntoVisualTree(wtf.OverlayGrid);
- _page = wtf.Content as PhoneApplicationPage;
- if (_page != null)
- {
- _page.BackKeyPress += OnBackKeyPress;
- }
- }
- else throw new InvalidOperationException("Root must be of WilcoxTransitionFrame type.");
- }
- private void OnBackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
- {
- e.Cancel = true;
- var handler = PressedBack;
- if (handler != null)
- {
- handler(this, EventArgs.Empty);
- }
- CloseWindow();
- }
- protected void CloseWindow()
- {
- // Remove from the parent visual tree.
- var me = this;
- if (_page != null)
- {
- _page.BackKeyPress -= OnBackKeyPress;
- _page = null;
- }
- Action removeVisualFromParent = () =>
- {
- Dispatcher.BeginInvoke(() =>
- {
- Panel p = me.Parent as Panel;
- if (p != null)
- {
- p.Children.Remove(me);
- }
- });
- };
- Action removeOverlay = () =>
- {
- Grid overlay = _overlay;
- _overlay = null;
- if (overlay != null)
- {
- Panel p = overlay.Parent as Panel;
- if (p != null)
- {
- p.Children.Remove(overlay);
- }
- }
- };
- Action removeVisuals = () =>
- {
- removeOverlay();
- removeVisualFromParent();
- };
- // Animate.
- var @out = GetTransitionOut();
- if (@out != null)
- {
- var transition = @out.GetTransition(_templateRoot);
- transition.Completed += (x, xe) =>
- {
- me.Opacity = 0;
- transition.Stop();
- removeVisuals();
- };
- transition.Begin();
- }
- else
- {
- removeVisuals();
- }
- }
- protected virtual TransitionElement GetTransitionIn()
- {
- return new SwivelTransition
- {
- Mode = SwivelTransitionMode.BackwardIn,
- };
- }
- protected virtual TransitionElement GetTransitionOut()
- {
- return new SwivelTransition
- {
- Mode = SwivelTransitionMode.BackwardOut,
- };
- }
- }
- }
Add Comment
Please, Sign In to add comment