How can I load style resources from a dynamically loaded Silverlight application (XAP)?

Posted by Tom on Stack Overflow See other posts from Stack Overflow or by Tom
Published on 2010-05-11T10:54:31Z Indexed on 2010/05/11 12:54 UTC
Read the original article Hit count: 726

Filed under:
|
|
|
|

I've followed Tim Heuer's video for dynamically loading other XAP's (into a 'master' Silverlight application), as well as some other links to tweak the loading of resources and am stuck on the particular issue of loading style resources from within the dynamically loaded XAP (i.e. the contents of Assets\Styles.xaml). When I run the master/hosting applcation, it successfully streams the dynamic XAP and I can read the deployment info etc. and load the assembly parts. However, when I actuall try to create an instance of a form from the Dynamic XAP, it fails with

Cannot find a Resource with the Name/Key LayoutRootGridStyle

which is in it's Assets\Styles.xaml file (it works if I run it directly so I know it's OK). For some reason these don't show up as application resources - not sure if I've totally got the wrong end of the stick, or am just missing something? Code snippet below (apologies it's a bit messy - just trying to get it working first) ...

'' # 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))
'' #...

any thoughts?

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about dynamic