从动态加载的模块调用用户控制方法

时间:2011-12-14 17:32:59

标签: wpf xaml mvvm code-injection

我对WPF,MVVM等都很陌生,所以请耐心等待。

另外,我使用VB而不是c# - 请不要评判我! :)

我正在按需加载模块,它将视图加载到视图模型中。此视图包含自定义控件,其中包含Web浏览器控件。

我的应用程序在我的模块中引发了事件,当一个特定的事件被引发时,我需要更改上面加载的这个webbrowser的URL。我不能为我的生活弄清楚如何创建一个我可以使用的控件的引用!

所以这里是相关的(我希望!)代码片段:

我有一个动态加载的模块,名为ExtensionSampleModuleVB:

Public Class ExtensionSampleModuleVB
        Implements IModule
        ReadOnly container As IObjectContainer
        ReadOnly viewManager As IViewManager
        ReadOnly commandManager As ICommandManager
        ReadOnly viewEventManager As IViewEventManager
        'WithEvents interactionManager As Interactions.IInteractionManager
        ReadOnly agentManager As IAgent

        Private WithEvents tmrTimer As New System.Timers.Timer(500)

        Public Sub New(ByVal container As IObjectContainer, ByVal viewManager As IViewManager, ByVal commandManager As ICommandManager, _
                       ByVal viewEventManager As IViewEventManager, ByVal agentManager As IAgent)
            Me.container = container
            Me.viewManager = viewManager
            Me.commandManager = commandManager
            Me.viewEventManager = viewEventManager
            Me.agentManager = agentManager
        End Sub

        Public Sub Initialize() Implements IModule.Initialize

            tmrTimer.Start()

            ' Replacing an already existing view "DispositionCodeView" in the Interaction Voice (not the behavior)

            ' Associate the existing "IDispositionCodeView" with the new "DispositionCodeExView" implementation
            container.RegisterType(Of IDispositionCodeView, DispositionCodeExViewVB)()


            ' Here we register the view (GUI) "IMySampleView" and its behavior counterpart "IMySamplePresentationModel"
            container.RegisterType(Of IMySampleViewVB, MySampleViewVB)()
            container.RegisterType(Of IMySampleViewModelVB, MySampleViewModelVB)()

            ' Put the MySample view in the region "ToolbarWorkplaceRegion" (The TabControl in the main toolbar)
            viewManager.ViewsByRegionName("ToolbarWorkplaceRegion").Insert(0, New ViewActivator() With { _
              .ViewType = GetType(IMySampleViewVB), _
              .ViewName = "MySampleVB" _
            })

            ' Here we register the view (GUI) "IMySampleMenuView"
            container.RegisterType(Of IMySampleMenuViewVB, MySampleMenuViewVB)()

            ' Put the MySampleMenuView view in the region "WorkspaceMenuRegion" (The Workspace toggle button in the main toolbar)
            viewManager.ViewsByRegionName("WorkspaceMenuRegion").Insert(0, New ViewActivator() With { _
              .ViewType = GetType(IMySampleMenuViewVB), _
              .ViewName = "MySampleMenuVB", _
              .ActivateView = True _
            })

// ...continued...

我的XAML控件如下:

<UserControl x:Class="ExtensionSampleVB.MySampleVB.MySampleViewVB"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     MinHeight="90">

    <Grid>
        <WebBrowser x:Name="webBrowser" Source="http://www.google.com" 
          Width="600" Height="600"  />
    </Grid>
</UserControl>

此控件背后的代码包含以下子代码,我需要调用'SetURLValue'方法,并传递一个URI,以便更新Web浏览器:

Public Sub SetURL_orig(ByVal strURL As Uri) 'Implements IMySampleViewVB.SetURL

    webBrowser.Navigate(strURL)

End Sub

Private Delegate Sub SetBrowserURI_Delegate(ByVal webURI As Uri)

Private Sub SetURLValue(ByVal uriAddress As System.Uri)

    If Dispatcher.CheckAccess Then
        'webBrowser.Dispatcher.BeginInvoke(Me.SetBrowserURI, System.Windows.Threading.DispatcherPriority.Background, uriAddress)

        SetURL_orig(uriAddress)

    Else

        Me.Dispatcher.BeginInvoke(New SetBrowserURI_Delegate(AddressOf SetURL_orig), uriAddress)

    End If

End Sub

有人可以帮忙吗?你还需要我的代码吗? 控件被加载到视图中,我没有任何问题。它只是从我的模块中调用这个方法,这对我来说很难。

我感谢任何人都可以提供帮助! 干杯, 米克

0 个答案:

没有答案