【问题标题】:Using Prism in ElementHost在 ElementHost 中使用 Prism
【发布时间】:2012-03-26 12:08:51
【问题描述】:

任何人都可以建议以下代码中可能缺少的内容。我的 WinForm 上没有显示任何内容。

我已经查看了有关此主题的以下相关帖子,但它们并没有为我解决问题。

带 ElementHost 的 WinForm

    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

【问题讨论】:

    标签: vb.net winforms prism


    【解决方案1】:

    我没有足够重视这篇文章 How to use Prisim within an ElementHost

    问题是您需要一个不为 null 且 不等于 typeof(Application) 的 Application.Current。

    因此我创建了以下类

    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
    

    【讨论】:

      猜你喜欢
      • 2010-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多