gpt4 book ai didi

c# - XAML 中的固定定位

转载 作者:太空宇宙 更新时间:2023-11-03 13:15:53 26 4
gpt4 key购买 nike

我有一个 Windows 应用商店风格的 WPF 应用程序,我刚刚向其中添加了搜索功能。当我单击应用栏中的“搜索”按钮时,我将包含 SearchBoxFlyoutPresenter 设置为 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 链接,但它们并没有真正帮助:

http://social.msdn.microsoft.com/Forums/en-US/9779328a-a7cd-447d-a4ac-bcc952083f43/fixed-positioning-in-wpf?forum=wpf

http://social.msdn.microsoft.com/Forums/windowsapps/en-US/7349d01d-dc0e-4e1c-9327-df90e00fbebf/how-to-handle-the-appearance-of-the-onscreen-keyboard?forum=winappswithcsharp

Popup control moves with parent

最佳答案

您可以通过一种非常简单的方式在 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>

(很抱歉,如果代码示例有一些语法错误,我是即时编写的)wpf 也有一些控件显示在所有其他控件之上,这些元素是上下文菜单、工具提示和装饰器,您也可以尝试它们。

希望这些想法对您有所帮助。

关于c# - XAML 中的固定定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26209580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com