Always show the navigation arrows on a FlipView control in UWP

旧巷老猫 提交于 2019-12-08 11:09:02

问题


When using a mouse and hovering over a Universal Windows Platform (UWP) FlipView control the previous/next navigation buttons are shown.

However, when using touch or pen, they remain hidden which may confuse users into not expecting additional content.

I would like navigation buttons to always be visible. How can I do this?


回答1:


We need to create a custom FlipView. Start with the code shown below:

public class CustomFlipView : FlipView
{
    protected override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        SelectionChanged += (s,e) => UpdateNavigationButtonsVisibility();

        Button prev = GetTemplateChild("MyPreviousButtonHorizontal") as Button;
        prev.Click += OnBack;
        prev.Visibility = Visibility.Collapsed;

        Button next = GetTemplateChild("MyNextButtonHorizontal") as Button;
        next.Click += OnNext;
        next.Visibility = Visibility.Collapsed;
    }

    private void OnBack(object sender, RoutedEventArgs e)
    {
        if (Items.Any())
        {
            SelectedIndex--;
            UpdateNavigationButtonsVisibility();
        }
    }

    private void OnNext(object sender, RoutedEventArgs e)
    {
        if (Items.Any())
        {
            SelectedIndex++;
            UpdateNavigationButtonsVisibility();
        }
    }

    private void UpdateNavigationButtonsVisibility()
    {
        Button prev = GetTemplateChild("MyPreviousButtonHorizontal") as Button;
        Button next = GetTemplateChild("MyNextButtonHorizontal") as Button;

        if (SelectedIndex < 1)
            prev.Visibility = Visibility.Collapsed;
        if (SelectedIndex == Items.Count - 1)
            next.Visibility = Visibility.Collapsed;
        if (Items.Count > 1 && SelectedIndex != Items.Count - 1)
            next.Visibility = Visibility.Visible;
        if (Items.Count > 1 && SelectedIndex > 0)
            prev.Visibility = Visibility.Visible;
    }
}

Build the project before opening the XAML file where you need to insert the CustomFlipView. Then add the control to your XAML; you may need to add namespace declaration depending where you created the CustomFlipView class.

The problem with the default FlipView is the scroll buttons reveal themselves only when pointing over it, something we do not have in case of a touch device. To make things more complicated, internal code sets the template’s buttons opacity to zero when loaded, so we need to change the name of both navigation buttons so the internal code gracefully degrades and allows them to be visible all the time. Then we add code to handle when the navigation buttons are clicked so we don't show previous button on first page or show next button on last page.

To overwrite the names of navigation buttons, we need to edit the control's template. The quickest way is by opening the Document Outline pane, right-clicking your CustomFlipViewEdit TemplateEdit a Copy.

A lot of code will appear in your XAML, but the important part to find looks like this:

<Button x:Name="MyPreviousButtonHorizontal" HorizontalAlignment="Left" Height="70" IsTabStop="False" Template="{StaticResource HorizontalPreviousTemplate}" UseSystemFocusVisuals="False" VerticalAlignment="Center" Width="40"/>
<Button x:Name="MyNextButtonHorizontal" HorizontalAlignment="Right" Height="70" IsTabStop="False" Template="{StaticResource HorizontalNextTemplate}" UseSystemFocusVisuals="False" VerticalAlignment="Center" Width="40"/>

Note I renamed the x:Name properties from PreviousButtonHorizontal to MyPreviousButtonHorizontal and NextButtonHorizontal to MyNextButtonHorizontal to match with the code-behind we wrote earlier.

Now the CustomFlipView should display navigation arrows when using touch and pen, not just mouse.



来源:https://stackoverflow.com/questions/54813880/always-show-the-navigation-arrows-on-a-flipview-control-in-uwp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!