本文介绍了WindowsFormsHost甚至通过其他WPF形式也失去了对激活应用程序的关注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重现我的案子(.net 4.0)

  1. 创建一个WPF应用程序(MainWindow.xaml)
  2. 添加一个包含文本框的Winform用户控件(UserConrol1.cs-Winform)
  3. 使用Windowsformshost
  4. 添加另一个包含文本框(wpf)的WPF窗口以进行投影(Window1.xaml)
  5. 在MainWindow InitializeComponent之后创建并显示Window1

您的项目已准备就绪,

  1. 运行Project并设置专注于MainWindow.xaml(在WindowsFormsHost中)的文本框
  2. 通过打开一个窗口(Windows文件浏览器,记事本,winamp等)来停用您的应用程序.
  3. 通过用鼠标单击文本框,尝试在Window1窗口中的文本框中编写文本

而且您会看到您无法将焦点设置在Window1中的文本框上,因为MainWindow Texbox(在winformshost中将使您的焦点转移到已激活的应用程序上)

有什么主意吗?

MainWindow.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"      标题="MainWindow".高度="350".宽度="525"   < Grid>      < my:WindowsFormsHost  Focusable ="False"  >         < local:UserControl1>         </local:UserControl1>      </my:WindowsFormsHost>   </Grid></Window> 

MainWindow.xaml.cs

命名空间WinFormsHostFocusProblem {   公共局部类MainWindow:Window    {     公共MainWindow()     {          InitializeComponent();          Window1 window1 = new Window1();          window1.Show();      }   }} 


 

Window1.xaml

< Window x:Class =" WinFormsHostFocusProblem.Window1"        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"       SizeToContent =" WidthAndHeight"         ResizeMode ="NoResize".       Topmost ="True"      标题="Window1".高度="300".宽度="300".背景=红色"   < Grid>      < TextBox Height =" asd</TextBox>   </Grid></Window> 
 


解决方案


To Reproduce my case (.net 4.0)

  1. Create a WPF Application (MainWindow.xaml)
  2. Add a Winform user control that contains a textbox (UserConrol1.cs - Winform)
  3. Put UserControl1 into MainWindow.xaml with windowsformshost
  4. Add another WPF Window that contains a textbox(wpf) to project (Window1.xaml)
  5. Create and Show Window1 after MainWindow InitializeComponent

Your project is ready,

  1. Run Project and set textbox focused in MainWindow.xaml (that in WindowsFormsHost)
  2. Deactivate your application by opening a window (Windows file explorer ,notepad, winamp etc.)
  3. Try to write in textbox that in Window1 window by clicking textbox with mouse

And you will see that you can't set focus on textbox in Window1 because MainWindow Texbox( in winformshost will steal your focus on you application got activating)

Any idea?

MainWindow.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>

MainWindow.xaml.cs

namespace WinFormsHostFocusProblem{   public partial class MainWindow : Window   {      public MainWindow()      {         InitializeComponent();         Window1 window1 = new Window1();         window1.Show();      }   }}


Window1.xaml

<Window x:Class="WinFormsHostFocusProblem.Window1"        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"        SizeToContent="WidthAndHeight"         ResizeMode="NoResize"        Topmost="True"        Title="Window1" Height="300" Width="300" Background="Red">    <Grid>      <TextBox Height="25">asd</TextBox>   </Grid></Window>


解决方案


这篇关于WindowsFormsHost甚至通过其他WPF形式也失去了对激活应用程序的关注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 18:37