黑马程序员技术交流社区
标题:
自定义windows phone 手机silverLight程序的弹出提示框
[打印本页]
作者:
sunrise2
时间:
2014-8-11 18:16
标题:
自定义windows phone 手机silverLight程序的弹出提示框
public sealed class PopupBox : FrameworkElement
{
private static readonly double _screenWidth = Application.Current.Host.Content.ActualWidth;
private static readonly double _screenHeight = Application.Current.Host.Content.ActualHeight;
private Popup _popup;
private PhoneApplicationFrame _frame;
private PhoneApplicationPage _page;
private Grid _grid;
#region DependencyProperty UIElement Child
public UIElement Child
{
get { return (UIElement)GetValue(ChildProperty); }
set { SetValue(ChildProperty, value); }
}
// Using a DependencyProperty as the backing store for Child. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ChildProperty =
DependencyProperty.Register("Child", typeof(UIElement), typeof(PopupBox), new PropertyMetadata(null));
#endregion DependencyProperty UIElement Child
#region DependencyProperty bool IsFrameNavigatingHandled
public bool IsFrameNavigatingHandled
{
get { return (bool)GetValue(IsFrameNavigatingHandledProperty); }
set { SetValue(IsFrameNavigatingHandledProperty, value); }
}
// Using a DependencyProperty as the backing store for IsFrameNavigatingHandled. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsFrameNavigatingHandledProperty =
DependencyProperty.Register("IsFrameNavigatingHandled", typeof(bool), typeof(PopupBox), new PropertyMetadata(false));
#endregion DependencyProperty bool IsFrameNavigatingHandled
#region DependencyProperty bool IsBackKeyHandled
public bool IsBackKeyHandled
{
get { return (bool)GetValue(IsBackKeyHandledProperty); }
set { SetValue(IsBackKeyHandledProperty, value); }
}
// Using a DependencyProperty as the backing store for IsBackKeyHandled. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsBackKeyHandledProperty =
DependencyProperty.Register("IsBackKeyHandled", typeof(bool), typeof(PopupBox), new PropertyMetadata(true));
#endregion DependencyProperty bool IsBackKeyHandled
#region DependencyProperty SupportedOrientation SupportedOrientation
public SupportedOrientation SupportedOrientation
{
get { return (SupportedOrientation)GetValue(SupportedOrientationProperty); }
set { SetValue(SupportedOrientationProperty, value); }
}
// Using a DependencyProperty as the backing store for SupportedOrientation. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SupportedOrientationProperty =
DependencyProperty.Register("SupportedOrientation", typeof(SupportedOrientation), typeof(PopupBox), new PropertyMetadata(SupportedOrientation.Horizontal | SupportedOrientation.Vertical));
#endregion DependencyProperty SupportedOrientation SupportedOrientation
#region DependencyProperty bool IsOpen
public bool IsOpen
{
get { return (bool)GetValue(IsOpenProperty); }
set { SetValue(IsOpenProperty, value); }
}
// Using a DependencyProperty as the backing store for IsOpen. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsOpenProperty =
DependencyProperty.Register("IsOpen", typeof(bool), typeof(PopupBox), new PropertyMetadata(false, OnIsOpenChanged));
private static void OnIsOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue == e.OldValue)
return;
if ((bool)e.NewValue)
((PopupBox)d).Show();
else
((PopupBox)d).Close();
}
#endregion DependencyProperty bool IsOpen
public bool IsCenter
{
get { return (bool)GetValue(IsCenterProperty); }
set { SetValue(IsCenterProperty, value); }
}
// Using a DependencyProperty as the backing store for IsCenter. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsCenterProperty =
DependencyProperty.Register("IsCenter", typeof(bool), typeof(PopupBox), new PropertyMetadata(false));
public event EventHandler OnOpened;
public event EventHandler OnClosed;
public event EventHandler OnFrameNavigaging;
public PopupBox()
{
_popup = new Popup();
_grid = new Grid();
_grid.Background = new SolidColorBrush(Color.FromArgb(127, 0, 0, 0));
}
/// <summary>
/// Popup this Box.
/// </summary>
private void Show()
{
if (_popup.IsOpen)
{
return;
}
_popup.RenderTransform = GetTransform();
// 在此只做了支持横屏的
_grid.Width = _screenHeight;
_grid.Height = _screenWidth;
_grid.Children.Add(Child);
_popup.Child = _grid;
_popup.IsOpen = true;
if (OnOpened != null)
{
OnOpened(this, EventArgs.Empty);
}
_frame = Application.Current.RootVisual as PhoneApplicationFrame;
if (_frame != null)
{
_page = _frame.Content as PhoneApplicationPage;
if (_page != null)
{
_page.OrientationChanged += OnOrientationChanged;
_page.BackKeyPress += OnBackKeyPress;
}
_frame.Navigating += FrameNavigating;
}
}
private void FrameNavigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
{
if (IsFrameNavigatingHandled)
{
if (OnFrameNavigaging != null)
{
OnFrameNavigaging(this, e);
}
}
}
/// <summary>
/// Cancel the popup on backkey pressed.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnBackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
if (!IsBackKeyHandled)
return;
e.Cancel = true;
Close();
}
private void Close()
{
_popup.IsOpen = false;
if (OnClosed != null)
{
OnClosed(this, EventArgs.Empty);
}
_page.BackKeyPress -= OnBackKeyPress;
_page.OrientationChanged -= OnOrientationChanged;
_frame.Navigating -= FrameNavigating;
}
/// <summary>
/// Change the popup's orientation when page's orientation change.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnOrientationChanged(object sender, OrientationChangedEventArgs e)
{
if (_popup != null)
{
Transform transform = GetTransform();
_popup.RenderTransform = transform;
}
}
public static Transform GetTransform()
{
PageOrientation orientation = GetPageOrientation();
switch (orientation)
{
case PageOrientation.Landscape:
case PageOrientation.LandscapeLeft:
return new CompositeTransform()
{
Rotation = 90,
TranslateX = _screenWidth
};
case PageOrientation.LandscapeRight:
return new CompositeTransform()
{
Rotation = -90,
TranslateY = _screenHeight
};
default:
return new CompositeTransform()
{
Rotation = 90,
TranslateX = _screenWidth
};
}
}
private static PageOrientation GetPageOrientation()
{
PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
if (frame != null)
{
PhoneApplicationPage page = frame.Content as PhoneApplicationPage;
if (page != null)
{
return page.Orientation;
}
}
return PageOrientation.None;
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2