Silverlight 创建表单实例时,XAML解析器无法在动态加载的XAP中找到资源

Silverlight 创建表单实例时,XAML解析器无法在动态加载的XAP中找到资源,silverlight,dynamic,resources,load,xap,Silverlight,Dynamic,Resources,Load,Xap,我已经跟随Tim Heuer的视频动态加载其他XAP(到“主”Silverlight应用程序中),以及一些其他链接来调整资源的加载,我一直关注从动态加载的XAP(即Assets\Styles.xaml的内容)中加载样式资源的特定问题。当我运行master/hosting应用程序时,它成功地流式传输动态XAP,我可以读取部署信息等并加载部件。然而,当我实际尝试从动态XAP创建表单实例时,它失败了 找不到名为/Key LayoutRootGridStyle的资源 它位于它的Assets\Styles

我已经跟随Tim Heuer的视频动态加载其他XAP(到“主”Silverlight应用程序中),以及一些其他链接来调整资源的加载,我一直关注从动态加载的XAP(即Assets\Styles.xaml的内容)中加载样式资源的特定问题。当我运行master/hosting应用程序时,它成功地流式传输动态XAP,我可以读取部署信息等并加载部件。然而,当我实际尝试从动态XAP创建表单实例时,它失败了

找不到名为/Key LayoutRootGridStyle的资源

它位于它的Assets\Styles.xaml文件中(如果我直接运行它,它就会工作,所以我知道它是可以的)。出于某种原因,这些并没有显示为应用程序资源-不确定我是否完全弄错了方向,或者只是遗漏了什么?下面的代码片段(很抱歉有点混乱-只是想先让它工作起来)

只是为了重新考虑,有3种情况需要考虑… 1) 动态加载的XAP样式资源保留在合并的资源字典中(在单独的xaml文件中),从动态加载的silverlight应用程序(XAP)的app.xaml中引用-运行主应用程序时,动态XAP中的资源在当前应用程序下似乎不存在(加载XAP部件后)。发生错误

2) 动态加载的XAP样式资源从合并资源字典(从单独的xaml文件)移动到动态应用程序的app.xaml中,以代替合并资源字典引用。-运行主应用程序时,动态XAP中的资源似乎存在于当前应用程序下(加载XAP部件后)。但是,错误仍然存在


3) 动态加载的XAP样式资源被复制到调用/主应用程序的app.xaml中(不可取)。-错误不再发生。

bykinag于…提供的答案

加载程序集后,我添加了以下行

App.Current.Resources.MergedDictionaries.Add(带有{.Source=newURI(“/MyTest;component/Assets/Styles.xaml”,UriKind.RelativeOrAbsolute)的新ResourceDictionary())


我现在遇到一个问题,动态应用程序似乎看不到其中的其他页面(找不到页面),但如果我无法解决这个问题,我可能会单独提出。

顺便说一句,如果我将样式资源直接移动到动态加载的XAP的app.xaml中(并删除对它们所在文件的合并词典引用)然后,它们突然出现在调用应用程序资源列表中。然而,即使我现在可以看到它们,我仍然从xaml解析器中得到“找不到名为/Key LayoutRootGridStyle的资源”错误(是的,字典键LayoutRootGridStyle在当前的应用程序资源中明确存在)。有趣的是,如果我将合并资源字典的内容从动态XAP文件直接复制到调用/master App.xaml的资源字典中,这似乎是可行的。但显然我不想这样做,因为我希望能够从动态XAML中获取所需的资源,而不是在主应用程序中“硬编码”它们。
'' # Here's the code that reads the dynamic XAP from the web server ...
'' #...
wCli = New WebClient
AddHandler wCli.OpenReadCompleted, AddressOf OpenXAPCompleted
wCli.OpenReadAsync(New Uri("MyTest.xap", UriKind.Relative))
'' #...

'' #Here's the sub that's called when openread is completed 
'' #...
Private Sub OpenXAPCompleted(ByVal sender As Object, ByVal e As      System.Net.OpenReadCompletedEventArgs)
Dim sManifest As String = New StreamReader(Application.GetResourceStream(New     StreamResourceInfo(e.Result, Nothing), New Uri("AppManifest.xaml", UriKind.Relative)).Stream).ReadToEnd

Dim deploymentRoot As XElement = XDocument.Parse(sManifest).Root
Dim deploymentParts As List(Of XElement) = _
        (From assemblyParts In deploymentRoot.Elements().Elements() Select assemblyParts).ToList()

Dim oAssembly As Assembly = Nothing
For Each xElement As XElement In deploymentParts
    Dim asmPart As AssemblyPart = New AssemblyPart()
    Dim source As String = xElement.Attribute("Source").Value
    Dim sInfo As StreamResourceInfo = Application.GetResourceStream(New StreamResourceInfo(e.Result, "application/binary"), New Uri(source, UriKind.Relative))
    If source = "MyTest.dll" Then
        oAssembly = asmPart.Load(sInfo.Stream)
    Else
        asmPart.Load(sInfo.Stream)
    End If
Next

Dim t As Type() = oAssembly.GetTypes()

Dim AppClass = (From parts In t Where parts.FullName.EndsWith(".App") Select parts).SingleOrDefault()
Dim mykeys As Array

If Not AppClass Is Nothing Then
    Dim a As Application = DirectCast(oAssembly.CreateInstance(AppClass.FullName), Application)

    For Each strKey As String In a.Resources.Keys
        If Not Application.Current.Resources.Contains(strKey) Then
            Application.Current.Resources.Add(strKey, a.Resources(strKey))
        End If
    Next
End If

Dim objectType As Type = oAssembly.GetType("MyTest.MainPage")
Dim ouiel = Activator.CreateInstance(objectType)
Dim myData As UIElement = DirectCast(ouiel, UIElement)
Me.splMain.Children.Add(myData)
Me.splMain.UpdateLayout()
End Sub
'' #...

'' # And here's the line that fails with "Cannot find a Resource with the Name/Key      LayoutRootGridStyle"
'' #  ...
System.Windows.Application.LoadComponent(Me, New     System.Uri("/MyTest;component/MainPage.xaml", System.UriKind.Relative))
'' #...