Flyout behind Appbar

2019-02-24 18:21发布

When you want your app to expand to the full screen (including status bar and appbar), you have to do :

var applicationView = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView();
applicationView.SetDesiredBoundsMode(Windows.UI.ViewManagement.ApplicationViewBoundsMode.UseCoreWindow);

Then if you want to have a flyout in your appbar or anywhere in your app, they will be displayed behind the appbar :

    <Page.BottomAppBar>
        <CommandBar>
            <AppBarButton Icon="Preview" Label="Preview">
                <AppBarButton.Flyout>
                    <MenuFlyout>
                        <MenuFlyoutItem Text="Fit width" />
                        <MenuFlyoutItem Text="Fit height" />
                        <MenuFlyoutItem Text="Fit page" />
                    </MenuFlyout>
                </AppBarButton.Flyout>
            </AppBarButton>
        </CommandBar>
    </Page.BottomAppBar>

The result :

http://i.stack.imgur.com/oWAj9.png

Same thing with flyouts on an item in a listview. They will be displayed behind the appbar :

enter image description here

How can i display the flyouts on top of the appbar ?

1条回答
smile是对你的礼貌
2楼-- · 2019-02-24 18:45

I can't seem to solve my issue (or someone who can help). So i did it that way, if it can help someone :

<Page.BottomAppBar>
        <CommandBar>
            <AppBarButton Icon="Preview" Label="Preview">
                <AppBarButton.Flyout>
                    <MenuFlyout Opened="MenuFlyout_Opened" Closed="MenuFlyout_Closed">
                        <MenuFlyoutItem Text="Fit width" />
                        <MenuFlyoutItem Text="Fit height" />
                        <MenuFlyoutItem Text="Fit page" />
                    </MenuFlyout>
                </AppBarButton.Flyout>
            </AppBarButton>
        </CommandBar>
    </Page.BottomAppBar>

private void MenuFlyout_Opened(object sender, object e)
{
  BottomAppBar.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
}

private void MenuFlyout_Closed(object sender, object e)
{
  BottomAppBar.Visibility = Windows.UI.Xaml.Visibility.Visible;
}

Now my flyout appears fully, since there is no more appbar. For mvvm list view items, i did it in a behavior action :

<DataTemplate x:Key="MvvmItemTemplate">
        <Grid>

            <i:Interaction.Behaviors>
                <icore:EventTriggerBehavior EventName="Holding">
                    <local:OpenFlyoutAction />
                </icore:EventTriggerBehavior>
            </i:Interaction.Behaviors>

            <FlyoutBase.AttachedFlyout>
                <MenuFlyout>
                    <MenuFlyoutItem ..... Command="{Binding MarkRead}" />
                    <MenuFlyoutItem ..... Command="{Binding MarkUnread}" />
                    <MenuFlyoutItem ..... Command="{Binding PinToStart}" />
                </MenuFlyout>
            </FlyoutBase.AttachedFlyout>
        </Grid>
    </DataTemplate>




public class OpenFlyoutAction : DependencyObject, IAction
    {
        public object Execute(object sender, object parameter)
        {
            // Show menu
            FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender);

            // sometimes the appbar is stuck behind the appbar, so hide the appbar
            (sender as FrameworkElement).GetFirstAncestorOfType<Page>().BottomAppBar.Visibility = Visibility.Collapsed;

            // show the appbar again when flyout is closed
            var flyout = FlyoutBase.GetAttachedFlyout((FrameworkElement)sender);
            EventHandler<object> showBar = null;
            showBar = delegate (object s, object e)
            {
                (sender as FrameworkElement).GetFirstAncestorOfType<Page>().BottomAppBar.Visibility = Visibility.Visible;
                // unsubscribe handler:
                flyout.Closed -= showBar;
            };
            flyout.Closed += showBar;

            return null;
        }
    }
查看更多
登录 后发表回答