Skip to content

Instantly share code, notes, and snippets.

@kw0006667
Created October 20, 2012 18:03
Show Gist options
  • Save kw0006667/3924222 to your computer and use it in GitHub Desktop.
Save kw0006667/3924222 to your computer and use it in GitHub Desktop.
在主頁面的程式碼中,加入控制項的呼叫
public sealed partial class MainPage : Page
{
public Popup headerMenuPopup;
// 自訂的控制項
public HeaderMenuControl headerMenuCtl;
// 是否已加入到 Popup 物件中
public bool isAddedToPopup;
public MainPage()
{
this.InitializeComponent();
// 初始化自訂控制項,並給予寬度和高度的初始值(和在控制項的設定為一樣的值)
this.headerMenuCtl = new HeaderMenuControl() { Width = 250, Height = 210 };
this.isAddedToPopup = false;
}
public static Popup ShowPopup(FrameworkElement source, UserControl control)
{
// 宣告一個 Popup 物件
Popup flyout = new Popup();
var windowBounds = Window.Current.Bounds;
var rootVisual = Window.Current.Content;
GeneralTransform gt = source.TransformToVisual(rootVisual);
// 設定絕對位置的原點從 (0, 0) 開始算
var absolutePosition = gt.TransformPoint(new Point(0, 0));
control.Measure(new Size(Double.PositiveInfinity, double.PositiveInfinity));
// 設定 Menu 要顯示的位置
flyout.VerticalOffset = absolutePosition.Y + source.ActualHeight;
flyout.HorizontalOffset = absolutePosition.X;
flyout.IsLightDismissEnabled = true;
// 將我們自訂的控制項加到此 Popup 物件中
flyout.Child = control;
// 設定 Menu 出現的動畫效果
var transitions = new TransitionCollection();
transitions.Add(new PopupThemeTransition() { FromHorizontalOffset = 0, FromVerticalOffset = -50 });
flyout.ChildTransitions = transitions;
return flyout;
}
private void pageTitle_Click_1(object sender, RoutedEventArgs e)
{
// 若是第一次按,則將物件新增到 Popup 物件裡。
if (!this.isAddedToPopup)
{
this.headerMenuPopup = ShowPopup(this.pageTitle, this.headerMenuCtl);
this.isAddedToPopup = true;
}
// 顯示 Popup
this.headerMenuPopup.IsOpen = true;
}
/// <summary>
/// 在此頁面即將離開時叫用。
/// </summary>
/// <param name="e">描述在離開頁面後要執行的事件資料。
/// Parameter 屬性通常用來設定頁面。</param>
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment