Trong quá trình vận hành phần mềm kiểm thử màn hình lớn, nhóm sản xuất phát hiện lỗi tiếp nhận chạm không ổn định trên ứng dụng WPF. Vấn đề xảy ra với cả .NET Framework 4.8 và .NET 6, đặc trưng là có thể sử dụng chuột nhưng không phản hồi chạm khi cửa sổ phụ xuất hiện.
Để tái hiện vấn đề, xây dựng demo đơn giản với logic: Khi cửa sổ chính (MainWindow) load, sau 2 giây sẽ hiển thị cửa sổ phụ (Window1) trong 10 giây rồi tự động đóng.
public partial class WindowChinh : Window
{
public WindowChinh()
{
InitializeComponent();
btn.Background = Brushes.Red;
Loaded += CuaSoChinh_Load;
StylusDown += CuaSoChinh_StylusDown;
MouseDown += CuaSoChinh_MouseDown;
}
private void CuaSoChinh_MouseDown(object sender, MouseButtonEventArgs e)
{
App.Log.Info("MouseDown ghi nhận");
}
private void CuaSoChinh_StylusDown(object sender, StylusDownEventArgs e)
{
App.Log.Info("StylusDown ghi nhận");
}
private async void CuaSoChinh_Load(object sender, RoutedEventArgs e)
{
await Task.Delay(2000);
var win = new CuaSoPhu();
win.Owner = this;
win.Closed += CuaSoPhu_Closed;
win.Show();
await Task.Delay(10000);
win.Close();
}
private void CuaSoPhu_Closed(object sender, EventArgs e)
{
grid1.Visibility = Visibility.Visible;
}
private void Btn_Click(object sender, RoutedEventArgs e)
{
App.Log.Info("Nút nhấn được kích hoạt");
if (btn.Background == Brushes.Green)
btn.Background = Brushes.Red;
else if (btn.Background == Brushes.Red)
btn.Background = Brushes.Green;
}
}
<Window
x:Class="WpfApp.WindowChinh"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Cửa sổ chính"
Width="800"
Height="450"
WindowState="Maximized">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="2*" />
</Grid.RowDefinitions>
<Grid x:Name="grid1" Visibility="Collapsed">
<Button x:Name="btn" Width="200" Height="50" HorizontalAlignment="Center" VerticalAlignment="Center" Click="Btn_Click" />
</Grid>
</Grid>
</Window>
<Window
x:Class="WpfApp.CuaSoPhu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Cửa sổ phụ"
Width="800"
Height="450"
Topmost="True"
WindowState="Maximized">
<Grid />
</Window>
namespace WpfApp
{
public partial class CuaSoPhu : Window
{
public CuaSoPhu()
{
InitializeComponent();
}
}
}
Quy trình tái hiện lỗi: Giữ hai tay trên cửa sổ phụ trong 10 giây cho đến khi tự động đóng. Sau đó cửa sổ chính mất hoàn toàn khả năng tiếp nhận chạm, thời gian phục hồi dao động từ 1 phút đến 15 phút tùy thiết bị.
Sử dụng công cụ ManipulationDemo để kiểm tra, phát hiện:
- Không nhận được sự kiện Down/Move/Up của chạm
- Có thể nhận được WM_Pointer message
- Chuột vẫn hoạt động bình thường
Phân tích qua Dnspy trong lớp PenThreadWorker (PresentationCore):
- Số lượng PenContexts và handles đều bằng 2 → Gọi GetPenEventMultiple
- Phương thức GetPenEventMultiple trả về giá trị 0
- Phương thức FireEvent không được kích hoạt do array = null
Giải pháp tạm thời:
- Bật hỗ trợ Pointer message:
AppContext.SetSwitch("Switch.System.Windows.Input.Stylus.EnablePointerSupport", true);
Lưu ý: Cần xử lý lại hệ tọa độ màn hình khi sử dụng
- Thay thế phương thức đóng cửa sổ bằng cách ẩn:
win.Hide(); // Thay vì win.Close();
Tránh xung đột với bộ đếm PenContexts