A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. public sealed class PopupBox : FrameworkElement
  2. {
  3.     private static readonly double _screenWidth = Application.Current.Host.Content.ActualWidth;
  4.     private static readonly double _screenHeight = Application.Current.Host.Content.ActualHeight;

  5.     private Popup _popup;
  6.     private PhoneApplicationFrame _frame;
  7.     private PhoneApplicationPage _page;
  8.     private Grid _grid;

  9.     #region DependencyProperty UIElement Child

  10.     public UIElement Child
  11.     {
  12.         get { return (UIElement)GetValue(ChildProperty); }
  13.         set { SetValue(ChildProperty, value); }
  14.     }

  15.     // Using a DependencyProperty as the backing store for Child.  This enables animation, styling, binding, etc...
  16.     public static readonly DependencyProperty ChildProperty =
  17.         DependencyProperty.Register("Child", typeof(UIElement), typeof(PopupBox), new PropertyMetadata(null));

  18.     #endregion DependencyProperty UIElement Child

  19.     #region DependencyProperty bool IsFrameNavigatingHandled

  20.     public bool IsFrameNavigatingHandled
  21.     {
  22.         get { return (bool)GetValue(IsFrameNavigatingHandledProperty); }
  23.         set { SetValue(IsFrameNavigatingHandledProperty, value); }
  24.     }

  25.     // Using a DependencyProperty as the backing store for IsFrameNavigatingHandled.  This enables animation, styling, binding, etc...
  26.     public static readonly DependencyProperty IsFrameNavigatingHandledProperty =
  27.         DependencyProperty.Register("IsFrameNavigatingHandled", typeof(bool), typeof(PopupBox), new PropertyMetadata(false));

  28.     #endregion DependencyProperty bool IsFrameNavigatingHandled

  29.     #region DependencyProperty bool IsBackKeyHandled

  30.     public bool IsBackKeyHandled
  31.     {
  32.         get { return (bool)GetValue(IsBackKeyHandledProperty); }
  33.         set { SetValue(IsBackKeyHandledProperty, value); }
  34.     }

  35.     // Using a DependencyProperty as the backing store for IsBackKeyHandled.  This enables animation, styling, binding, etc...
  36.     public static readonly DependencyProperty IsBackKeyHandledProperty =
  37.         DependencyProperty.Register("IsBackKeyHandled", typeof(bool), typeof(PopupBox), new PropertyMetadata(true));

  38.     #endregion DependencyProperty bool IsBackKeyHandled

  39.     #region DependencyProperty SupportedOrientation SupportedOrientation

  40.     public SupportedOrientation SupportedOrientation
  41.     {
  42.         get { return (SupportedOrientation)GetValue(SupportedOrientationProperty); }
  43.         set { SetValue(SupportedOrientationProperty, value); }
  44.     }

  45.     // Using a DependencyProperty as the backing store for SupportedOrientation.  This enables animation, styling, binding, etc...
  46.     public static readonly DependencyProperty SupportedOrientationProperty =
  47.         DependencyProperty.Register("SupportedOrientation", typeof(SupportedOrientation), typeof(PopupBox), new PropertyMetadata(SupportedOrientation.Horizontal | SupportedOrientation.Vertical));

  48.     #endregion DependencyProperty SupportedOrientation SupportedOrientation

  49.     #region DependencyProperty bool IsOpen

  50.     public bool IsOpen
  51.     {
  52.         get { return (bool)GetValue(IsOpenProperty); }
  53.         set { SetValue(IsOpenProperty, value); }
  54.     }

  55.     // Using a DependencyProperty as the backing store for IsOpen.  This enables animation, styling, binding, etc...
  56.     public static readonly DependencyProperty IsOpenProperty =
  57.         DependencyProperty.Register("IsOpen", typeof(bool), typeof(PopupBox), new PropertyMetadata(false, OnIsOpenChanged));

  58.     private static void OnIsOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  59.     {
  60.         if (e.NewValue == e.OldValue)
  61.             return;
  62.         if ((bool)e.NewValue)
  63.             ((PopupBox)d).Show();
  64.         else
  65.             ((PopupBox)d).Close();
  66.     }

  67.     #endregion DependencyProperty bool IsOpen

  68.     public bool IsCenter
  69.     {
  70.         get { return (bool)GetValue(IsCenterProperty); }
  71.         set { SetValue(IsCenterProperty, value); }
  72.     }

  73.     // Using a DependencyProperty as the backing store for IsCenter.  This enables animation, styling, binding, etc...
  74.     public static readonly DependencyProperty IsCenterProperty =
  75.         DependencyProperty.Register("IsCenter", typeof(bool), typeof(PopupBox), new PropertyMetadata(false));

  76.     public event EventHandler OnOpened;

  77.     public event EventHandler OnClosed;

  78.     public event EventHandler OnFrameNavigaging;

  79.     public PopupBox()
  80.     {
  81.         _popup = new Popup();
  82.         _grid = new Grid();
  83.         _grid.Background = new SolidColorBrush(Color.FromArgb(127, 0, 0, 0));
  84.     }

  85.     /// <summary>
  86.     /// Popup this Box.
  87.     /// </summary>
  88.     private void Show()
  89.     {
  90.         if (_popup.IsOpen)
  91.         {
  92.             return;
  93.         }

  94.         _popup.RenderTransform = GetTransform();
  95.         // 在此只做了支持横屏的
  96.         _grid.Width = _screenHeight;
  97.         _grid.Height = _screenWidth;

  98.         _grid.Children.Add(Child);
  99.         _popup.Child = _grid;
  100.         _popup.IsOpen = true;

  101.         if (OnOpened != null)
  102.         {
  103.             OnOpened(this, EventArgs.Empty);
  104.         }

  105.         _frame = Application.Current.RootVisual as PhoneApplicationFrame;
  106.         if (_frame != null)
  107.         {
  108.             _page = _frame.Content as PhoneApplicationPage;
  109.             if (_page != null)
  110.             {
  111.                 _page.OrientationChanged += OnOrientationChanged;
  112.                 _page.BackKeyPress += OnBackKeyPress;
  113.             }
  114.             _frame.Navigating += FrameNavigating;
  115.         }
  116.     }

  117.     private void FrameNavigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
  118.     {
  119.         if (IsFrameNavigatingHandled)
  120.         {
  121.             if (OnFrameNavigaging != null)
  122.             {
  123.                 OnFrameNavigaging(this, e);
  124.             }
  125.         }
  126.     }

  127.     /// <summary>
  128.     /// Cancel the popup on backkey pressed.
  129.     /// </summary>
  130.     /// <param name="sender"></param>
  131.     /// <param name="e"></param>
  132.     private void OnBackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
  133.     {
  134.         if (!IsBackKeyHandled)
  135.             return;

  136.         e.Cancel = true;
  137.         Close();
  138.     }

  139.     private void Close()
  140.     {
  141.         _popup.IsOpen = false;
  142.         if (OnClosed != null)
  143.         {
  144.             OnClosed(this, EventArgs.Empty);
  145.         }
  146.         _page.BackKeyPress -= OnBackKeyPress;
  147.         _page.OrientationChanged -= OnOrientationChanged;
  148.         _frame.Navigating -= FrameNavigating;
  149.     }

  150.     /// <summary>
  151.     /// Change the popup's orientation when page's orientation change.
  152.     /// </summary>
  153.     /// <param name="sender"></param>
  154.     /// <param name="e"></param>
  155.     private void OnOrientationChanged(object sender, OrientationChangedEventArgs e)
  156.     {
  157.         if (_popup != null)
  158.         {
  159.             Transform transform = GetTransform();
  160.             _popup.RenderTransform = transform;
  161.         }
  162.     }

  163.     public static Transform GetTransform()
  164.     {
  165.         PageOrientation orientation = GetPageOrientation();

  166.         switch (orientation)
  167.         {
  168.             case PageOrientation.Landscape:
  169.             case PageOrientation.LandscapeLeft:
  170.                 return new CompositeTransform()
  171.                 {
  172.                     Rotation = 90,
  173.                     TranslateX = _screenWidth
  174.                 };
  175.             case PageOrientation.LandscapeRight:
  176.                 return new CompositeTransform()
  177.                 {
  178.                     Rotation = -90,
  179.                     TranslateY = _screenHeight
  180.                 };
  181.             default:
  182.                 return new CompositeTransform()
  183.                 {
  184.                     Rotation = 90,
  185.                     TranslateX = _screenWidth
  186.                 };
  187.         }
  188.     }

  189.     private static PageOrientation GetPageOrientation()
  190.     {
  191.         PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
  192.         if (frame != null)
  193.         {
  194.             PhoneApplicationPage page = frame.Content as PhoneApplicationPage;

  195.             if (page != null)
  196.             {
  197.                 return page.Orientation;
  198.             }
  199.         }
  200.         return PageOrientation.None;
  201.     }
  202. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马