WindowStyle None 和触摸输入 [英] WindowStyle None and touch input

查看:16
本文介绍了WindowStyle None 和触摸输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Windows 10, when using touch device like Surface, touching an input control like a TextBox in a not maximized window, causes moving the whole window up, so that the user can see what he is typing (if keyboard is docked). But it does not happen when WindowStyle is set to none. Why it does not happen? We need this behavior in our app. Can it be fixed on WindowStyle=None? I found it's not connected with it's style - there are no build in triggers or something. We need WindowsStyle=None for custom close button bar (we want the bar to be transparent, only the button is visible).

推荐答案

我遇到了类似的问题,试图让我的应用程序正确响应 Windows 10 触摸键盘的存在,但除非 WindowStyle 设置为 None 以外的其他值.

I faced a similar issue trying to get my application to react to the presence of the Windows 10 touch keyboard properly, but it wouldn't do so unless WindowStyle was set to something other than None.

为此苦苦挣扎了一段时间,直到我决定自己尝试设置 Window 的样式并手动移除边框,这让我发现了 WindowChrome 类.

Struggled with it for a while, until I decided to just try and style the Window myself and removing the borders manually, which led me to discover the WindowChrome class.

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

自定义 WPF 应用程序窗口外观的一种方法是将 Window.WindowStyle 属性设置为 None.这会从窗口中删除非客户框架,只留下客户区域,您可以在该区域应用自定义样式.但是,当非客户端框架被移除时,您也会失去它提供的系统功能和行为,例如标题按钮和窗口大小调整. 另一个副作用是当窗口被移除时会覆盖 Windows 任务栏最大化.设置 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.

因此,似乎由于缺少此非客户端(或操作系统)框架,它会导致应用程序无法对键盘的存在做出反应.

So it seems that because this non-client (or OS) frame is missing, it causes the application to not be able to react to the keyboards presence.

解决方案是实现自定义WindowChrome.可以这样做:

The solution is to implement a custom WindowChrome. This can be done like this:

<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",那么根据上面的文档,Window 逻辑必须是自定义的.幸运的是,Microsoft 提供了一个很好的示例,说明如何编写自定义逻辑以对触摸键盘的存在做出反应:查看此链接.

If you absolutely must use WindowStyle="None", then according to the documentation above, the Window logic must be custom. Fortunately there's a nice example from Microsoft on how to write custom logic to react to the presence of a touch keyboard: See this link.

如果您想将自己的自定义按钮添加到标题栏,请查看 这篇优秀的文章关于你的窗口样式.

If you would like to add your own custom buttons to the title bar, check out this excellent post about styling your Window.

希望这会有所帮助!

更新:

经过进一步调查,发现此方案仅适用于 Windows 10 版本1903.在我们在生产中使用的 1809 上进行尝试时,它不起作用.原因显然是微软已经改变了应用程序在全屏模式下对触摸键盘的反应方式.

After further investigation, it turned out that this solution only worked on Windows 10 version 1903. When tried on 1809 which we use in production, it didn't work. The reason is that apparently Microsoft have changed the way applications react to the touch keyboard when in full-screen mode.

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

This can be easily tested by maximizing Explorer in both versions of Windows, and to see how in 1809 nothing happens, but in 1903 the Window is resized to fit the remaining space on the screen.

我注意到的另一件重要的事情是,当我从 Visual Studio 启动应用程序时(无论是否附加了调试器),当触摸屏出现时,UI 不会对其做出反应,但是 当我从资源管理器运行可执行文件时,它确实可以工作.所以在我的测试中,我总是会构建,然后去 binDebug,然后从那里启动 exe.

Another important thing I noticed is that when I start the application from Visual Studio (whether debugger is attached or not), when the touch screen shows up, the UI doesn't react to it, but when I run the executable from explorer, then it does work. So in my testing I would always build, then go to binDebug, and start the exe from there.

这篇关于WindowStyle None 和触摸输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆