问题
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:
There are 2 ways:
1) You can set the app or the page to auto-resize layout using
Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().SetDesiredBoundsMode(Windows.UI.ViewManagement.ApplicationViewBoundsMode.UseVisible);
However by doing this, AppBar will affect layout if ClosedDisplayMode changes...
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);
}
}
}
}
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).
Hope it helps!
来源:https://stackoverflow.com/questions/29345748/handle-the-new-windows-phone-software-buttons-winrt