C# XAML中的固定定位
我有一个Windows应用商店风格的WPF应用程序,我刚刚添加了搜索功能。当我单击应用程序栏中的搜索按钮时,我会将包含C# XAML中的固定定位,c#,wpf,xaml,C#,Wpf,Xaml,我有一个Windows应用商店风格的WPF应用程序,我刚刚添加了搜索功能。当我单击应用程序栏中的搜索按钮时,我会将包含搜索框的FlyoutPresenter设置为可见。此按钮位于右下角。它在有键盘的计算机上运行良好,但当虚拟键盘或InputPane打开时,我遇到了一个问题。首先,键盘盖住了盒子。当框处于焦点时,我通过检查和调整框的边距解决了这个问题,但是当我将页面滚动到顶部和底部时,控件开始在页面上移动。以下是我的最低代码: XAML: 我需要的是该框不受用户在屏幕上滚动的影响。在HTML中,这
搜索框的FlyoutPresenter
设置为可见。此按钮位于右下角。它在有键盘的计算机上运行良好,但当虚拟键盘或InputPane
打开时,我遇到了一个问题。首先,键盘盖住了盒子。当框处于焦点时,我通过检查和调整框的边距解决了这个问题,但是当我将页面滚动到顶部和底部时,控件开始在页面上移动。以下是我的最低代码:
XAML:
我需要的是该框不受用户在屏幕上滚动的影响。在HTML中,这称为固定定位。我已经读到,在XAML中,这在本质上是不可能的,但是有一些变通方法。我已经阅读了这些MSDN和SO链接,但它们并没有真正起到帮助作用:
您可以用一种非常简单的方式模拟XAML中的固定行为:
<Grid Background="White" x:Name="MainGrid">
<ContentControl VerticalAligment="Stretch" HorizontalAligment="Stretch">
<!--All other visual controls, the float item will be located over all controls located here, even scrolls viewers-->
</ContentControl>
<!-- Float item -->
<SomeControl>
<!--The control you want be over in the fixed position,
you can set the layout to it, and locate it where you want
just set the Vertical/Horizontal Aligment, margin, height, width-->
</SomeControl>
</Grid>
(很抱歉,如果代码示例有一些sintax错误,我必须立即编写)
此外,wpf还有一些控件显示在所有其他控件之上的层上,这些元素是上下文菜单、工具提示和装饰器,您也可以尝试它们
我希望这些想法能有所帮助。我肯定会对此进行研究。我确实知道工具提示,但当鼠标被拿走时,它们就会消失,而这需要保持打开状态,直到用户关闭它。在我写了这个问题之后,我才知道装饰器,但我已经研究过了,而且它们在Windows应用商店应用程序上还不受支持。我还没有查看工具提示,但我会这么做。它似乎不起作用。当InputPane打开时,整个网格向上移动。它在我的系统上的任何其他应用程序中都不会像这样工作,因此必须有一种方法使InputPane在网格上滑动,而不是向上推。现在,这就是我正在调查的。好吧,让大家都知道,我从来没有解决过这个问题,但我通过在输入窗格打开时将搜索框移到页面顶部来解决问题。如有任何答复,将不胜感激。
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;
}
}
<Grid Background="White" x:Name="MainGrid">
<ContentControl VerticalAligment="Stretch" HorizontalAligment="Stretch">
<!--All other visual controls, the float item will be located over all controls located here, even scrolls viewers-->
</ContentControl>
<!-- Float item -->
<SomeControl>
<!--The control you want be over in the fixed position,
you can set the layout to it, and locate it where you want
just set the Vertical/Horizontal Aligment, margin, height, width-->
</SomeControl>
</Grid>