处理新的 Windows Phone 软件按钮 (WinRT) [英] handle the new windows phone software buttons (WinRT)

查看:22
本文介绍了处理新的 Windows Phone 软件按钮 (WinRT)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处理弹出的覆盖我的应用程序内容的 Windows Phone 软件按钮的最佳方法是什么.以前他们有这些硬件按钮(返回、Windows、搜索按钮),但现在在某些设备中他们引入了软件键.例如 Lumia730 设备.

What is the best way to handle the windows phone software buttons that pop up overlaying my app content.Earlier they had these hardware buttons(Back, Windows,Search Buttons) but now in some devices they have introduced software keys.Example Lumia 730 device.

推荐答案

有两种方式:

1) 您可以使用

    Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().SetDesiredBoundsMode(Windows.UI.ViewManagement.ApplicationViewBoundsMode.UseVisible);

然而这样做,如果 ClosedDisplayMode 改变,AppBar 将影响布局...

However by doing this, AppBar will affect layout if ClosedDisplayMode changes...

2) 为了解决问题 1,我创建了以下帮助类,它会通知是否显示软件按钮,并给出被软件按钮遮挡的页面高度.这允许我调整仅受软件按钮影响/隐藏的页面内容.

2) To overcome problem 1, I've created the following helper class, which notifies if software buttons are being shown or not, and also gives the page's height that's being occluded by software buttons. This allows me to adjust page contents exclusively affected/hidden by software buttons.

/// <summary>
/// Handles software buttons events on Windows Phone
/// Use this to control to show occluded parts if software buttons are being shown
/// and Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().DesiredBoundsMode are set to "UseCoreWindow"
/// </summary>
public static class SoftwareButtonsHelper
{
    public delegate void SoftwareButtonsChangedDelegate(Visibility softwareButtonsVisibility, double diffSize);

    /// <summary>
    /// Raised when software buttons visibility changes
    /// </summary>
    public static event SoftwareButtonsChangedDelegate SoftwareButtonsChanged;

    /// <summary>
    /// Current window bottom size
    /// </summary>
    private static double currentBottonSize = 0.0;

    /// <summary>
    /// Are software buttons being monitored?
    /// </summary>
    public static bool Listening { get; private set; }

    /// <summary>
    /// Software buttons visibility
    /// </summary>
    public static Visibility SoftwareButtonsVisibility { get; private set; }

    /// <summary>
    /// Current page height that's being occluded
    /// </summary>
    public static double CurrentInvisibleDifference { get; private set; }

    /// <summary>
    /// Start listening for software buttons
    /// (event will raise if they appear/disappear)
    /// </summary>
    public static void Listen()
    {
        if (!Listening)
        {
            currentBottonSize = ApplicationView.GetForCurrentView().VisibleBounds.Bottom;
            ApplicationView.GetForCurrentView().VisibleBoundsChanged += (ApplicationView sender, object args) =>
            {
                if (currentBottonSize != ApplicationView.GetForCurrentView().VisibleBounds.Bottom)
                {
                    currentBottonSize = ApplicationView.GetForCurrentView().VisibleBounds.Bottom;

                    var currentPageAppBar = ((Window.Current.Content as Frame).Content as Page).BottomAppBar;
                    var isAppBarVisible = currentPageAppBar != null && currentPageAppBar.Visibility == Visibility.Visible;
                    var diff = Window.Current.Bounds.Bottom - currentBottonSize;
                    var diffAppBar = diff;
                    if (isAppBarVisible)
                    {
                        diffAppBar = Math.Round(diff - currentPageAppBar.Height);
                        diff = diffAppBar;
                    }
                    else
                    {
                        diff = Math.Round(diff);
                    }

                    if ((isAppBarVisible && diffAppBar == 0)
                    || (!isAppBarVisible && diff == 0))
                    {
                        // Either contents are visible or are occluded by app bar, do nothing..
                        OnSoftwareButtonsChanged(Visibility.Collapsed, diff);
                    }
                    else
                    {
                        // Software buttons are active...
                        OnSoftwareButtonsChanged(Visibility.Visible, diff);
                    }
                }
            };
            Listening = true;
        }
    }

    /// <summary>
    /// Raise event
    /// </summary>
    /// <param name="newVisibility"></param>
    /// <param name="difference"></param>
    private static void OnSoftwareButtonsChanged(Visibility newVisibility, double difference)
    {
        CurrentInvisibleDifference = difference;
        if (SoftwareButtonsVisibility != newVisibility)
        {
            SoftwareButtonsVisibility = newVisibility;
            if (SoftwareButtonsChanged != null)
            {
                SoftwareButtonsChanged(newVisibility, difference);
            }
        }
    }
}

您只需要调用 SoftwareButtonsHelper.Listen();之后 Window.Current.Activate();在 App.xaml.cs 上并在目标页面上订阅 SoftwareButtonsHelper.SoftwareButtonsChanged.

You just need to call SoftwareButtonsHelper.Listen(); AFTER Window.Current.Activate(); on App.xaml.cs and subscribe SoftwareButtonsHelper.SoftwareButtonsChanged on the target(s) page(s).

希望有帮助!

这篇关于处理新的 Windows Phone 软件按钮 (WinRT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆