wpf - WindowStyle None 和触摸输入

标签 wpf textbox windows-10 touch

在 Windows 10 中,当使用 Surface 等触摸设备时,在未最大化的窗口中触摸文本框等输入控件会导致整个窗口向上移动,以便用户可以看到他正在输入的内容(如果键盘停靠) .但当 WindowStyle 设置为 none 时,它不会发生。为什么它没有发生? 我们的应用程序需要这种行为。它可以在 WindowStyle=None 上修复吗?我发现它与它的风格无关 - 没有内置触发器或其他东西。我们需要 WindowsStyle=None 来自定义关闭按钮栏(我们希望栏是透明的,只有按钮是可见的)。 WindowStyle None and touch input enter image description here

最佳答案

我遇到了一个类似的问题,试图让我的应用程序对 Windows 10 触摸键盘的存在做出正确的 react ,但它不会这样做,除非 WindowStyle 被设置为 以外的东西>无

挣扎了一段时间,直到我决定自己尝试设置窗口样式并手动删除边框,这让我发现了 WindowChrome 类。

WindowChrome documentation关于 WindowStyle="None" 提到了这个:

One way to customize the appearance of a WPF application window is to set the Window.WindowStyle property to None. This removes the non-client frame from the window and leaves only the client area, to which you can apply a custom style. However, when the non-client frame is removed, you also lose the system features and behaviors that it provides, such as caption buttons and window resizing. Another side effect is that the window will cover the Windows taskbar when it is maximized. Setting WindowStyle.None enables you to create a completely custom application, but also requires that you implement custom logic in your application to emulate standard window behavior.

所以看起来是因为缺少这个非客户端(或操作系统)框架,导致应用程序无法对键盘的存在使用react。

解决方案是实现自定义 WindowChrome。这可以像这样完成:

<Window x:Class="WpfApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:shell="clr-namespace:System.Windows.Shell;assembly=PresentationFramework"
    Title="MainWindow" Height="350" Width="525" WindowState="Maximized">
    <shell:WindowChrome.WindowChrome>
        <shell:WindowChrome NonClientFrameEdges="None"
                            UseAeroCaptionButtons="False"
                            CornerRadius="0"
                            GlassFrameThickness="0"
                            ResizeBorderThickness="0"
                            CaptionHeight="0" />
    </shell:WindowChrome.WindowChrome>

    <Grid Background="DarkOrange">
        <TextBlock Text="TEST" VerticalAlignment="Center" 
                   HorizontalAlignment="Center" FontSize="32" />
    </Grid>
</Window>

如果您绝对必须使用 WindowStyle="None",那么根据上面的文档,窗口逻辑必须是自定义的。幸运的是,Microsoft 提供了一个很好的示例,说明如何编写自定义逻辑来响应触摸键盘的存在:See this link .

如果您想将自己的自定义按钮添加到标题栏,请查看 this excellent post关于设置窗口样式。

希望这对您有所帮助!

更新:

经过进一步调查,发现此解决方案仅适用于 Windows 10 版本 1903。当我们在生产中使用的 1809 上尝试时,它没有用。原因很明显,微软已经改变了应用程序在全屏模式下对触摸键盘的 react 方式。

这可以通过在两个版本的 Windows 中最大化资源管理器来轻松测试,并查看在 1809 年没有任何反应,但在 1903 年窗口调整大小以适应屏幕上的剩余空间。

我注意到的另一件重要事情是,当我从 Visual Studio 启动应用程序时(无论是否连接调试器),当触摸屏出现时,UI 不会对其使用react,但是 当我从资源管理器运行可执行文件时,它确实有效。所以在我的测试中,我总是会构建,然后转到 bin\Debug,然后从那里启动 exe。

关于wpf - WindowStyle None 和触摸输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45185993/

相关文章:

uwp - 在uwp中是否无法在运行时更改启动屏幕?

user-interface - Windows 10 应用程序 - SplitView 或 Pivot?

php - 使用不同数量的文本框更新 mysql 数据库

c# - 左右粗边框

wpf - wpf datagrid,在 mvvm 场景中编辑单元格值

c# - 大型 RegEx 匹配导致程序挂起

WPF数据绑定(bind)文本框到另一个对象中的整数属性

c# - 在 TextBox 中启用除最后 N 个字符之外的密码字符/限制屏蔽字符

wpf - 在浏览器中运行 WPF 应用程序

wpf - DataContext 用于在 DataTemplate 中将 UserControl 与 ViewModel 绑定(bind)