【发布时间】:2023-04-08 00:48:01
【问题描述】:
我有一个 Windows 应用商店风格的 WPF 应用程序,我刚刚添加了搜索功能。当我单击应用栏中的搜索按钮时,我将包含SearchBox 的FlyoutPresenter 设置为Visible。此按钮位于右下角。它在带键盘的计算机上运行良好,但是当虚拟键盘或InputPane 打开时我遇到了问题。首先,键盘盖住了盒子。我通过在框处于焦点时检查和调整框的边距解决了这个问题,但是当我将页面滚动到最顶部和最底部时,控件开始在页面上移动。这是我的最小代码:
XAML:
<Grid Background="White" x:Name="MainGrid">
<!-- App Bar with Search button -->
<AppBar x:Name="BAppBar" VerticalAlignment="Bottom">
<CommandBar>
<CommandBar.PrimaryCommands>
<AppBarButton Icon="Find" Label="Search" Click="Search_Click"/>
</CommandBar.PrimaryCommands>
</CommandBar>
</AppBar>
<!-- Search button and Close button -->
<FlyoutPresenter VerticalAlignment="Top" Name="SearchPop" Visibility="Collapsed">
<StackPanel Orientation="Horizontal">
<SearchBox Name="Search" GotFocus="Search_Focus" LostFocus="Search_Focus"/>
<AppBarButton Name="SearchClose" Icon="Cancel" Click="Search_Close" />
</StackPanel>
</FlyoutPresenter>
</Grid>
C#:
public partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
// Close app bar, show search box, and set margin to bottom of page
private void Search_Click(object sender, RoutedEventArgs e)
{
BAppBar.IsOpen = false;
SearchPop.Visibility = Windows.UI.Xaml.Visibility.Visible;
SearchPop.Margin = new Thickness(0, MainGrid.ActualHeight - SearchPop.ActualHeight, 0, 0);
}
// Set margin for opening/closing virtual keyboard
private void Search_Focus(object sender, RoutedEventArgs e)
{
Windows.UI.ViewManagement.InputPane.GetForCurrentView().Showing += (s, args) =>
{
double flyoutOffset = (int)args.OccludedRect.Height - SearchPop.ActualHeight;
SearchPop.Margin = new Thickness(0, flyoutOffset, 0, 0);
};
Windows.UI.ViewManagement.InputPane.GetForCurrentView().Hiding += (s, args) =>
{
SearchPop.Margin = new Thickness(0, MainGrid.ActualHeight - SearchPop.ActualHeight, 0, 0);
};
}
// Close search
private void Search_Close(object sender, RoutedEventArgs e)
{
SearchPop.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
}
}
我需要的是让框不受用户在屏幕中滚动的影响。在 HTML 中,这称为固定定位。我已经读到它在 XAML 中是不可能的,但是有一些解决方法。我已经阅读了这些 MSDN 和 SO 链接,但它们并没有真正帮助:
【问题讨论】:
-
好的,只是为了让大家知道,我从来没有按原样解决过这个问题,但是我通过在 InputPane 打开时将搜索框移动到页面顶部来解决我的问题。任何答案将不胜感激。