【问题标题】:Fixed positioning in XAML修复了 XAML 中的定位
【发布时间】:2023-04-08 00:48:01
【问题描述】:

我有一个 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

【问题讨论】:

  • 好的,只是为了让大家知道,我从来没有按原样解决过这个问题,但是我通过在 InputPane 打开时将搜索框移动到页面顶部来解决我的问题。任何答案将不胜感激。

标签: c# wpf xaml


【解决方案1】:

您可以通过非常简单的方式模拟 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 只是滑过网格,而不是向上推。现在,这就是我正在研究的。到那时,其他一切都会变得容易。
猜你喜欢
  • 2010-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-09
  • 2011-11-25
  • 2013-08-18
相关资源
最近更新 更多