簡體   English   中英

從WPF中托管的WinForms控件創建表單 - 未設置所有者

[英]Creating a form from a WinForms Control hosted within WPF — Owner is not set

腳本

我有以下場景(我已將其剝離為示例應用程序):

  • 帶有窗口( MainWindow )的WPF應用程序,托管WinForms UserControl( UserControl1 )。
    • 該控件在后面的代碼中動態添加到WindowsFormsHost
  • UserControl1有一個打開表單的按鈕( Form1
    • 它使用form1.Show(this)

問題是:

  • Form1.Owner屬性為null。
    • 在實際應用程序中,完成了一些涉及.Owner屬性的工作,這意味着我不能忽略這個問題。 當然,理想情況下,這里沒有任何依賴性。
    • 在實際應用程序中,我無法控制此代碼的WinForms端。 我們的WPF應用程序正在托管另一個團隊的WinForms控件。
    • 筆記:
      • 當我使用WinForms主機時, .Owner屬性設置正確。
      • UserControl1以其他所有方式托管 - 在實際應用程序中,其他一切工作正常,只是用戶控件打開的表單沒有合適的所有者。

我能理解為什么這里有問題,但我希望我的下一個問題的答案可能是'是'!

有什么方法可以通過在等式的WPF方面進行更改來實現這一點嗎?

如果不這樣做,可以在WinForms方面做任何事情嗎? (這可能超出了我可以在那里實現一些變化的可能性......)

示例代碼

這是我的示例應用程序中的代碼。 首先是WPF方面:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="700">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button Name="btnAdd" Click="btnAdd_Click" Content="Add Winform"/>

        <WindowsFormsHost Grid.Row="1" Name="host" ScrollViewer.CanContentScroll="False"/>
    </Grid>
</Window>
public partial class MainWindow : Window
{
    private WindowsFormsHost host;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
        UserControl1 uc1 = new UserControl1();
        WindowsFormsHost.EnableWindowsFormsInterop();
        this.host.Child = uc1;

    }
}

而現在WinForms方面......

UserControl1只是一個用戶控件,上面有一個按鈕和一個標簽。 這是代碼:

public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();
            form1.Show(this);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.label1.Text = "this: " + this + Environment.NewLine + "parent: " + this.Parent + Environment.NewLine + "toplevelcontrol: " + this.TopLevelControl;
        }
    }

Form1只是一個空表單。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        MessageBox.Show(" this: " + this + " owner: " + this.Owner);
    }
}

在WPF中托管時,消息框中顯示的Owner和標簽中顯示的TopLevelControlnull ,但在另一個WinForms應用程序中托管時具有值。

進一步的調查

我想這里的問題是.OwnerForm類型,WPF應用程序中沒有這種類型的實例。 很難想象財產在這種情況下可能具有的有效價值。 因此,似乎我需要更改代碼,以便訪問Form1中的`.Owner'屬性。

<Window
  xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" 
>

<my:WindowsFormsHost  Name="MapHost" ScrollViewer.CanContentScroll="False"  SizeChanged="MapHost_SizeChanged" />

MapControl繼承System.Windows.Forms.Control

MapHost.Child = MapControl;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM