WindowsFormsHost甚至通过其他WPF形式也失去了对激活应用程序的关注 [英] WindowsFormsHost steals focus on activating application even by other wpf form

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

问题描述

  • 添加一个包含文本框的Winform用户控件(UserConrol1.cs-Winform)
  • 添加另一个包含文本框(wpf)的WPF窗口以进行投影(Window1.xaml)
  • 您的项目已准备就绪,

      运行Project并设置专注于MainWindow.xaml(在WindowsFormsHost中)的文本框
    1. 通过用鼠标单击文本框,尝试在Window1窗口中的文本框中编写文本

    有什么主意吗?

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

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


     

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

     解决方案
  • 您好yunusayd,

    按照您的逐步说明,我已经测试了您的代码,但是我无法解决您的问题.一切正常.

    对于这些类似的情况,我曾经吃过饭,请查看下面的反馈和主题,并注意,在您的情况下,Window1中的TextBox将成为焦点.

    http://social.msdn.microsoft .com/Forums/zh-CN/wpf/thread/30e21265-2c14-4b3d-bde4-df803dee3c3c

    希望有帮助.

    如果问题仍然存在,请提供有关如何进行修复的更多详细信息.

    谢谢.

    今天愉快.


    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>


    解决方案

    Hi yunusayd,

    I test your code, as you said step by step, but I can't repro your issue. Every thing works fine.  

     And for these similar kinds of situations, I have ever meat, please check out below feedback and thread, and payattention that in your situation, the TextBox in Window1 will get focus corecctly.

    https://connect.microsoft.com/VisualStudio/feedback/details/707184/textbox-in-pop-up-window-cant-get-keyboard-focus-after-some-controls-in-windowsformhost-lost-focus

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/30e21265-2c14-4b3d-bde4-df803dee3c3c

    Hope it helps.

    If issue still exist, please give a more detail information about how to repro it.

    Thank you.

    Have a nice day.


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

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