我正在为应用程序栏图标按钮实现我自己的弹出菜单(类似于PhoneFlipMenu工具)。我使用垂直StackPanel为我的弹出,我需要显示它与动画时,相应的应用程序栏按钮被点击。代码看起来像这样:
private void appBarIconButtonList_Click(object sender, EventArgs e)
{
ApplicationBar.IsVisible = false;
AnimatePopupMenuListCommands(true);
}
private void AnimatePopupMenuListCommands(bool openMenu)
{
PlaneProjection planeProjection = popupMenuListCommands.Projection as PlaneProjection;
DoubleAnimation anima = new DoubleAnimation();
if (openMenu)
{
anima.From = 90;
anima.To = 0;
}
else
{
anima.From = 0;
anima.To = 90;
}
anima.Duration = new Duration(TimeSpan.FromSeconds(0.1));
Storyboard.SetTarget(anima, planeProjection);
Storyboard.SetTargetProperty(anima, new PropertyPath(PlaneProjection.RotationXProperty));
Storyboard storyboard = new Storyboard();
storyboard.Children.Add(anima);
storyboard.Begin();
}
主要的问题是动画在应用程序栏隐藏之前就开始了。因此,弹出式菜单在那之后稍微跳了一下。如何运行动画后,应用程序栏已完全隐藏?
尝试在动画完成后隐藏应用程序栏。
storyboard.Completed += storyboard_Completed;
void storyboard_Completed(object sender, EventArgs e)
{
ApplicationBar.IsVisible = false;
}
您可以使用Dispatcher或DispatcherTimer等待appbar被隐藏。下面是一个使用Dispatcher的示例:
private void ApplicationBarIconButton_OnClick(object sender, EventArgs e)
{
ApplicationBar.IsVisible = false;
WaitForAppBarThenShowMenu();
}
private void WaitForAppBarThenShowMenu()
{
if (ApplicationBar.IsVisible)
{
Dispatcher.BeginInvoke(WaitForAppBarThenShowMenu);
}
else
{
AnimatePopupMenuListCommands();
}
}
旧答案-不工作我相信你可以订阅ApplicationBar的statechange事件,然后开始你的故事。
EventHandler<ApplicationBarStateChangedEventArgs> stateChanged = null;
stateChanged = (s,e) =>
{
ApplicationBar.StateChanged -= stateChanged;
AnimatePopupMenuListCommands(true);
};
ApplicationBar.StateChanged += stateChanged;
ApplicationBar.IsVisible = false;