简体   繁体   中英

Add dynamically loaded WindowsForms control to a WPF WindowsFormsHost

I have a WPF project where I load a WindowsForsm assembly (dll file) dynamically like this:

        Assembly currentAssembly = Assembly.LoadFile(System.IO.Directory.GetCurrentDirectory() + @"\" + fileName);            
        System.Windows.Forms.Control currentControl = (System.Windows.Forms.Control)currentAssembly.CreateInstance(featureName, false);

        featureHost.Child = currentControl;

My XAML code for the for the featureHost looks like this:

<WindowsFormsHost Name="featureHost" Grid.Row="1" Margin="14,4,14,14" Background="WhiteSmoke"></WindowsFormsHost>

The problem that I have is that in the C# code the currentControl will be null, but currentAssembly seasm to be loaded correctly. But if I add project reference to a project that is a Windows Forms project the currentControl getts the correct value. Of coures I don't want to have a reference to a WindowsForms project that I don't need. Dose anyone have any suggestions on why it works like this and how I can fix it? I'm using .Net 4.0.

Found the real problem. The trouble was not that a reference to a Windows Forms project was needed as I thought when I wrote the question. The real problem was the Windows Forms control that I was trying to load could not find a dll that it needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM