开发者

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 = As开发者_运维百科sembly.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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜