小编yun*_*nus的帖子

WindowsFormsHost窃取关注激活应用程序甚至通过应用程序的其他wpf表单激活

重现我的案例 (.net 4.0)

  1. 创建 WPF 应用程序 (MainWindow.xaml)
  2. 添加包含文本框的 Winform 用户控件 (UserConrol1.cs - Winform)
  3. 使用 windowsformshost 将 UserControl1 放入 MainWindow.xaml
  4. 将另一个包含文本框 (wpf) 的 WPF 窗口添加到项目 (Window1.xaml)
  5. 在 MainWindow InitializeComponent 之后创建并显示 Window1

你的项目准备好了,

  1. 运行项目并在 MainWindow.xaml 中设置文本框(在 WindowsFormsHost 中)
  2. 通过打开一个窗口(Windows 文件资源管理器、记事本、winamp 等)来停用您的应用程序
  3. 尝试通过用鼠标单击文本框在 Window1 窗口中的文本框中写入

你会看到你不能在 Window1 中将焦点设置在文本框上,因为 MainWindow Texbox(在 winformshost 中会窃取你对你的应用程序激活的关注)

任何的想法?

主窗口.xaml

<Window x:Class="WinFormsHostFocusProblem.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WinFormsHostFocusProblem"
        xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
      <my:WindowsFormsHost  Focusable="False"  >
         <local:UserControl1>

         </local:UserControl1>
      </my:WindowsFormsHost>

   </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

主窗口.xaml.cs

namespace WinFormsHostFocusProblem
{
   public partial class MainWindow : Window
   { …
Run Code Online (Sandbox Code Playgroud)

wpf windowsformshost focus-stealing

2
推荐指数
1
解决办法
3396
查看次数

标签 统计

focus-stealing ×1

windowsformshost ×1

wpf ×1