2012-03-26 67 views
0

任何人都可以提出以下代碼中可能缺少的內容。我在WinForm上沒有顯示任何內容。在ElementHost中使用棱鏡

我已經看過以下相關的帖子關於這個話題,但他們還沒有解決我的問題。

的WinForm與ElementHost的

Public Sub New() 

    ' This call is required by the designer. 
    InitializeComponent() 

    ' WPF application instance 
    _wpfApplication = Application.Current() 

    If Application.Current Is Nothing Then 
     _wpfApplication = New Application() 
    End If 

    ' Load the application modules 
    Dim unityBootstrapper As New Bootstrapper() 
    unityBootstrapper.Run() 

    ' Bind the shell to the ElementHost 
    Dim shellElement = unityBootstrapper.Container.Resolve(Of Shell)() 
    ehMaster.Child = shellElement 

End Sub 

引導程序

Public NotInheritable Class Bootstrapper 
Inherits UnityBootstrapper 

Protected Overrides Function CreateShell() As DependencyObject 
    Return New Shell 
End Function 

Protected Overrides Function GetModuleCatalog() As IModuleCatalog 
    Dim catalog As ModuleCatalog = New ConfigurationModuleCatalog() 
    catalog.AddModule(GetType(Pitchbook.Excel.ChartWizardModule)) 
    Return catalog 
End Function 

End Class 

殼牌

<UserControl x:Class="Shell" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:cal="http://www.codeplex.com/CompositeWPF"> 
<DockPanel> 
    <ItemsControl x:Name="HeaderRegion" cal:RegionManager.RegionName="HeaderRegion" DockPanel.Dock="Top" Height="auto"> 

    </ItemsControl> 
    <ItemsControl x:Name="LeftRegion" cal:RegionManager.RegionName="LeftRegion" Width="auto" DockPanel.Dock="Left"> 

    </ItemsControl> 
    <ItemsControl x:Name="MainRegion" cal:RegionManager.RegionName="MainRegion" DockPanel.Dock="Bottom"> 

    </ItemsControl> 
</DockPanel> 
</UserControl> 

Partial Public Class Shell 
Inherits UserControl 

Public Sub New() 
    InitializeComponent() 
End Sub 

End Class 

ChartWizardModule

Public NotInheritable Class ChartWizardModule 
Implements IModule 

Private ReadOnly regionManager As IRegionManager 

Public Sub Initialize() Implements IModule.Initialize 
    regionManager.RegisterViewWithRegion("MainRegion", GetType(Test)) 
End Sub 

Public Sub New(regionManager As IRegionManager) 
    Me.regionManager = regionManager 
End Sub 

End Class 

回答

0

我沒有給予足夠的重視這個帖子 How to use Prisim within an ElementHost

的問題是,你需要一個Application.Current那不是null並且是n不等於typeof(應用程序)

因此,我創建了以下類

Public Class WpfApplicationAdapter 
    Inherits Windows.Application 

    ' HACK - This class shouldn't be required. Look out for a resolution to this in future WPF versions. 

End Class 

而且改變了我的WinForm的構造函數以下

Public Sub New() 

    ' This call is required by the designer. 
    InitializeComponent() 

    If Application.Current Is Nothing Then 
     Dim wpfAppAdapter As New WpfApplicationAdapter 
    End If 

    ' Load the application modules 
    Dim formBootstrapper As New Bootstrapper 
    formBootstrapper.Run() 

    ' Get the current instance of shell and bind it to the ElementHost 
    Dim shellElement = formBootstrapper.Container.Resolve(Of Shell)() 
    ehMaster.Child = shellElement 

End Sub