我已经关注了 Tim Heuer 的视频,用于动态加载其他 XAP(到“主”Silverlight 应用程序中),以及一些其他链接来调整资源的加载,并被困在从动态加载中加载样式资源的特定问题上XAP(即 Assets\Styles.xaml 的内容)。当我运行主/托管应用程序时,它成功地流式传输动态 XAP,我可以读取部署信息等并加载装配部件。但是,当我实际尝试从 Dynamic XAP 创建表单实例时,它失败了
找不到具有名称/键 LayoutRootGridStyle 的资源
它在它的 Assets\Styles.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))
'' #...
再次概括一下,有 3 个场景需要考虑... 1) 动态加载的 XAP 样式资源留在合并的资源字典中(在单独的 xaml 文件中),从动态加载的 silverlight 应用程序的 app.xaml 中引用(XAP) - 运行主应用程序时,来自动态 XAP 的资源似乎不存在于当前应用程序下(在加载 XAP 程序集部件之后)。发生错误。
2) 动态加载的 XAP 的样式资源从合并的资源字典(从单独的 xaml 文件)移动到动态应用程序的 app.xaml 中,代替合并的资源字典引用。- 运行主应用程序时,来自动态 XAP DO 的资源似乎存在于当前应用程序下(在加载 XAP 程序集部件之后)。但是,错误仍然发生。
3) 将动态加载的 XAP 的样式资源复制到调用/主应用程序的 app.xaml 中(不可取)。- 不再发生错误。